llvm.org GIT mirror llvm / e0b3c33
Allow DataLayout to specify addrspace for allocas. LLVM makes several assumptions about address space 0. However, alloca is presently constrained to always return this address space. There's no real way to avoid using alloca, so without this there is no way to opt out of these assumptions. The problematic assumptions include: - That the pointer size used for the stack is the same size as the code size pointer, which is also the maximum sized pointer. - That 0 is an invalid, non-dereferencable pointer value. These are problems for AMDGPU because alloca is used to implement the private address space, which uses a 32-bit index as the pointer value. Other pointers are 64-bit and behave more like LLVM's notion of generic address space. By changing the address space used for allocas, we can change our generic pointer type to be LLVM's generic pointer type which does have similar properties. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299888 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 2 years ago
34 changed file(s) with 385 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
18111811 must be a multiple of 8-bits. If omitted, the natural stack
18121812 alignment defaults to "unspecified", which does not prevent any
18131813 alignment promotions.
1814 ``A
``
1815 Specifies the address space of objects created by '``alloca``'.
1816 Defaults to the default address space of 0.
18141817 ``p[n]:::``
18151818 This specifies the *size* of a pointer and its ```` and
18161819 ````\erred alignments for address space ``n``. All sizes are in
71917194
71927195 ::
71937196
7194 = alloca [inalloca] [, ] [, align ] ; yields type*:result
7197 = alloca [inalloca] [, ] [, align ] [, addrspace()] ; yields type addrspace(num)*:result
71957198
71967199 Overview:
71977200 """""""""
71997202 The '``alloca``' instruction allocates memory on the stack frame of the
72007203 currently executing function, to be automatically released when this
72017204 function returns to its caller. The object is always allocated in the
7202 default address space (address space zero).
7205 address space for allocas indicated in the datalayout.
72037206
72047207 Arguments:
72057208 """"""""""
103103 /// Defaults to false.
104104 bool BigEndian;
105105
106 unsigned AllocaAddrSpace;
106107 unsigned StackNaturalAlign;
107108
108109 enum ManglingModeT {
185186 clear();
186187 StringRepresentation = DL.StringRepresentation;
187188 BigEndian = DL.isBigEndian();
189 AllocaAddrSpace = DL.AllocaAddrSpace;
188190 StackNaturalAlign = DL.StackNaturalAlign;
189191 ManglingMode = DL.ManglingMode;
190192 LegalIntWidths = DL.LegalIntWidths;
240242 }
241243
242244 unsigned getStackAlignment() const { return StackNaturalAlign; }
245 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
243246
244247 bool hasMicrosoftFastStdCallMangling() const {
245248 return ManglingMode == MM_WinCOFFX86;
3232 #include "llvm/IR/Instructions.h"
3333 #include "llvm/IR/Intrinsics.h"
3434 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
3536 #include "llvm/IR/Operator.h"
3637 #include "llvm/IR/Type.h"
3738 #include "llvm/IR/Value.h"
10881089 // Instruction creation methods: Memory Instructions
10891090 //===--------------------------------------------------------------------===//
10901091
1092 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1093 Value *ArraySize = nullptr, const Twine &Name = "") {
1094 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1095 }
1096
10911097 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
10921098 const Twine &Name = "") {
1093 return Insert(new AllocaInst(Ty, ArraySize), Name);
1099 const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1100 return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
10941101 }
10951102 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
10961103 // converting the string to 'bool' for the isVolatile parameter.
6666 AllocaInst *cloneImpl() const;
6767
6868 public:
69 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
69 explicit AllocaInst(Type *Ty, unsigned AddrSpace,
70 Value *ArraySize = nullptr,
7071 const Twine &Name = "",
7172 Instruction *InsertBefore = nullptr);
72 AllocaInst(Type *Ty, Value *ArraySize,
73 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
7374 const Twine &Name, BasicBlock *InsertAtEnd);
7475
75 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
76 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
77
78 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
76 AllocaInst(Type *Ty, unsigned AddrSpace,
77 const Twine &Name, Instruction *InsertBefore = nullptr);
78 AllocaInst(Type *Ty, unsigned AddrSpace,
79 const Twine &Name, BasicBlock *InsertAtEnd);
80
81 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
7982 const Twine &Name = "", Instruction *InsertBefore = nullptr);
80 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
83 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
8184 const Twine &Name, BasicBlock *InsertAtEnd);
8285
8386 // Out of line virtual method, so the vtable, etc. has a home.
18511851 return false;
18521852 }
18531853
1854 /// ParseOptionalCommaAddrSpace
1855 /// ::=
1856 /// ::= ',' addrspace(1)
1857 ///
1858 /// This returns with AteExtraComma set to true if it ate an excess comma at the
1859 /// end.
1860 bool LLParser::ParseOptionalCommaAddrSpace(unsigned &AddrSpace,
1861 LocTy &Loc,
1862 bool &AteExtraComma) {
1863 AteExtraComma = false;
1864 while (EatIfPresent(lltok::comma)) {
1865 // Metadata at the end is an early exit.
1866 if (Lex.getKind() == lltok::MetadataVar) {
1867 AteExtraComma = true;
1868 return false;
1869 }
1870
1871 Loc = Lex.getLoc();
1872 if (Lex.getKind() != lltok::kw_addrspace)
1873 return Error(Lex.getLoc(), "expected metadata or 'addrspace'");
1874
1875 if (ParseOptionalAddrSpace(AddrSpace))
1876 return true;
1877 }
1878
1879 return false;
1880 }
1881
18541882 bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
18551883 Optional &HowManyArg) {
18561884 Lex.Lex();
60326060 /// (',' 'align' i32)?
60336061 int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
60346062 Value *Size = nullptr;
6035 LocTy SizeLoc, TyLoc;
6063 LocTy SizeLoc, TyLoc, ASLoc;
60366064 unsigned Alignment = 0;
6065 unsigned AddrSpace = 0;
60376066 Type *Ty = nullptr;
60386067
60396068 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
60476076 bool AteExtraComma = false;
60486077 if (EatIfPresent(lltok::comma)) {
60496078 if (Lex.getKind() == lltok::kw_align) {
6050 if (ParseOptionalAlignment(Alignment)) return true;
6079 if (ParseOptionalAlignment(Alignment))
6080 return true;
6081 if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
6082 return true;
6083 } else if (Lex.getKind() == lltok::kw_addrspace) {
6084 ASLoc = Lex.getLoc();
6085 if (ParseOptionalAddrSpace(AddrSpace))
6086 return true;
60516087 } else if (Lex.getKind() == lltok::MetadataVar) {
60526088 AteExtraComma = true;
60536089 } else {
60546090 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
6055 ParseOptionalCommaAlign(Alignment, AteExtraComma))
6091 ParseOptionalCommaAlign(Alignment, AteExtraComma) ||
6092 (!AteExtraComma &&
6093 ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma)))
60566094 return true;
60576095 }
60586096 }
60606098 if (Size && !Size->getType()->isIntegerTy())
60616099 return Error(SizeLoc, "element count must have integer type");
60626100
6063 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
6101 const DataLayout &DL = M->getDataLayout();
6102 unsigned AS = DL.getAllocaAddrSpace();
6103 if (AS != AddrSpace) {
6104 // TODO: In the future it should be possible to specify addrspace per-alloca.
6105 return Error(ASLoc, "address space must match datalayout");
6106 }
6107
6108 AllocaInst *AI = new AllocaInst(Ty, AS, Size, Alignment);
60646109 AI->setUsedWithInAlloca(IsInAlloca);
60656110 AI->setSwiftError(IsSwiftError);
60666111 Inst = AI;
245245 bool ParseOrdering(AtomicOrdering &Ordering);
246246 bool ParseOptionalStackAlignment(unsigned &Alignment);
247247 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
248 bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
249 bool &AteExtraComma);
248250 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
249251 bool parseAllocSizeArguments(unsigned &ElemSizeArg,
250252 Optional &HowManyArg);
40054005 }
40064006 if (!Ty || !Size)
40074007 return error("Invalid record");
4008 AllocaInst *AI = new AllocaInst(Ty, Size, Align);
4008
4009 // FIXME: Make this an optional field.
4010 const DataLayout &DL = TheModule->getDataLayout();
4011 unsigned AS = DL.getAllocaAddrSpace();
4012
4013 AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align);
40094014 AI->setUsedWithInAlloca(InAlloca);
40104015 AI->setSwiftError(SwiftError);
40114016 I = AI;
174174 // because the value needs to be added to the global context list.
175175 auto &DL = F.getParent()->getDataLayout();
176176 unsigned Align = DL.getPrefTypeAlignment(FunctionContextTy);
177 FuncCtx = new AllocaInst(FunctionContextTy, nullptr, Align, "fn_context",
178 &EntryBB->front());
177 FuncCtx = new AllocaInst(FunctionContextTy, DL.getAllocaAddrSpace(),
178 nullptr, Align, "fn_context", &EntryBB->front());
179179
180180 // Fill in the function context structure.
181181 for (LandingPadInst *LPI : LPads) {
8585 // All fields are reset by runOnFunction.
8686 EHPersonality Personality = EHPersonality::Unknown;
8787
88 const DataLayout *DL = nullptr;
8889 DenseMap BlockColors;
8990 MapVector> FuncletBlocks;
9091 };
110111 if (!isFuncletEHPersonality(Personality))
111112 return false;
112113
114 DL = &Fn.getParent()->getDataLayout();
113115 return prepareExplicitEH(Fn);
114116 }
115117
10691071 if (!isa(EHPad)) {
10701072 // If the EHPad isn't a terminator, then we can insert a load in this block
10711073 // that will dominate all uses.
1072 SpillSlot = new AllocaInst(PN->getType(), nullptr,
1074 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
10731075 Twine(PN->getName(), ".wineh.spillslot"),
10741076 &F.getEntryBlock().front());
10751077 Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
11561158 Function &F) {
11571159 // Lazilly create the spill slot.
11581160 if (!SpillSlot)
1159 SpillSlot = new AllocaInst(V->getType(), nullptr,
1161 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
11601162 Twine(V->getName(), ".wineh.spillslot"),
11611163 &F.getEntryBlock().front());
11621164
31163116 if (AI->getAlignment()) {
31173117 Out << ", align " << AI->getAlignment();
31183118 }
3119
3120 unsigned AddrSpace = AI->getType()->getAddressSpace();
3121 if (AddrSpace != 0) {
3122 Out << ", addrspace(" << AddrSpace << ')';
3123 }
3124
31193125 } else if (isa(I)) {
31203126 if (Operand) {
31213127 Out << ' ';
173173
174174 LayoutMap = nullptr;
175175 BigEndian = false;
176 AllocaAddrSpace = 0;
176177 StackNaturalAlign = 0;
177178 ManglingMode = MM_None;
178179 NonIntegralAddressSpaces.clear();
349350 break;
350351 case 'S': { // Stack natural alignment.
351352 StackNaturalAlign = inBytes(getInt(Tok));
353 break;
354 }
355 case 'A': { // Default stack/alloca address space.
356 AllocaAddrSpace = getInt(Tok);
357 if (!isUInt<24>(AllocaAddrSpace))
358 report_fatal_error("Invalid address space, must be a 24bit integer");
352359 break;
353360 }
354361 case 'm':
393400
394401 bool DataLayout::operator==(const DataLayout &Other) const {
395402 bool Ret = BigEndian == Other.BigEndian &&
403 AllocaAddrSpace == Other.AllocaAddrSpace &&
396404 StackNaturalAlign == Other.StackNaturalAlign &&
397405 ManglingMode == Other.ManglingMode &&
398406 LegalIntWidths == Other.LegalIntWidths &&
11981198 return Amt;
11991199 }
12001200
1201 AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1202 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
1203
1204 AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1205 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
1206
1207 AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
1201 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
12081202 Instruction *InsertBefore)
1209 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1210
1211 AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
1203 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
1204
1205 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
12121206 BasicBlock *InsertAtEnd)
1213 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1214
1215 AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
1207 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
1208
1209 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
12161210 const Twine &Name, Instruction *InsertBefore)
1217 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1218 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1219 AllocatedType(Ty) {
1211 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1212
1213 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1214 const Twine &Name, BasicBlock *InsertAtEnd)
1215 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1216
1217 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1218 unsigned Align, const Twine &Name,
1219 Instruction *InsertBefore)
1220 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1221 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1222 AllocatedType(Ty) {
12201223 setAlignment(Align);
12211224 assert(!Ty->isVoidTy() && "Cannot allocate void!");
12221225 setName(Name);
12231226 }
12241227
1225 AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
1226 const Twine &Name, BasicBlock *InsertAtEnd)
1227 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1228 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1228 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1229 unsigned Align, const Twine &Name,
1230 BasicBlock *InsertAtEnd)
1231 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1232 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
12291233 AllocatedType(Ty) {
12301234 setAlignment(Align);
12311235 assert(!Ty->isVoidTy() && "Cannot allocate void!");
38273831
38283832 AllocaInst *AllocaInst::cloneImpl() const {
38293833 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3834 getType()->getAddressSpace(),
38303835 (Value *)getOperand(0), getAlignment());
38313836 Result->setUsedWithInAlloca(isUsedWithInAlloca());
38323837 Result->setSwiftError(isSwiftError());
31803180 void Verifier::visitAllocaInst(AllocaInst &AI) {
31813181 SmallPtrSet Visited;
31823182 PointerType *PTy = AI.getType();
3183 Assert(PTy->getAddressSpace() == 0,
3184 "Allocation instruction pointer not in the generic address space!",
3183 // TODO: Relax this restriction?
3184 Assert(PTy->getAddressSpace() == DL.getAllocaAddrSpace(),
3185 "Allocation instruction pointer not in the stack address space!",
31853186 &AI);
31863187 Assert(AI.getAllocatedType()->isSized(&Visited),
31873188 "Cannot allocate unsized type", &AI);
158158 assert(PType && "Expecting pointer type in handleByValParam");
159159
160160 Type *StructType = PType->getElementType();
161 AllocaInst *AllocA = new AllocaInst(StructType, Arg->getName(), FirstInst);
161 unsigned AS = Func->getParent()->getDataLayout().getAllocaAddrSpace();
162 AllocaInst *AllocA = new AllocaInst(StructType, AS, Arg->getName(), FirstInst);
162163 // Set the alignment to alignment of the byval parameter. This is because,
163164 // later load/stores assume that alignment, and we are going to replace
164165 // the use of the byval parameter with this alloca instruction.
126126 // is spilled into the coroutine frame and recreate the alignment information
127127 // here. Possibly we will need to do a mini SROA here and break the coroutine
128128 // frame into individual AllocaInst recreating the original alignment.
129 auto *Frame = new AllocaInst(FrameTy, "", InsertPt);
129 const DataLayout &DL = F->getParent()->getDataLayout();
130 auto *Frame = new AllocaInst(FrameTy, DL.getAllocaAddrSpace(), "", InsertPt);
130131 auto *FrameVoidPtr =
131132 new BitCastInst(Frame, Type::getInt8PtrTy(C), "vFrame", InsertPt);
132133
348348 Call->eraseFromParent();
349349 }
350350
351 const DataLayout &DL = F->getParent()->getDataLayout();
352
351353 // Since we have now created the new function, splice the body of the old
352354 // function right into the new function, leaving the old rotting hulk of the
353355 // function empty.
375377
376378 // Just add all the struct element types.
377379 Type *AgTy = cast(I->getType())->getElementType();
378 Value *TheAlloca = new AllocaInst(AgTy, nullptr, "", InsertPt);
380 Value *TheAlloca = new AllocaInst(AgTy, DL.getAllocaAddrSpace(), nullptr,
381 "", InsertPt);
379382 StructType *STy = cast(AgTy);
380383 Value *Idxs[2] = {ConstantInt::get(Type::getInt32Ty(F->getContext()), 0),
381384 nullptr};
18181818 GS.AccessingFunction->doesNotRecurse() &&
18191819 isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,
18201820 LookupDomTree)) {
1821 const DataLayout &DL = GV->getParent()->getDataLayout();
1822
18211823 DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
18221824 Instruction &FirstI = const_cast(*GS.AccessingFunction
18231825 ->getEntryBlock().begin());
18241826 Type *ElemTy = GV->getValueType();
18251827 // FIXME: Pass Global's alignment when globals have alignment
1826 AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
1828 AllocaInst *Alloca = new AllocaInst(ElemTy, DL.getAllocaAddrSpace(), nullptr,
18271829 GV->getName(), &FirstI);
18281830 if (!isa(GV->getInitializer()))
18291831 new StoreInst(GV->getInitializer(), Alloca, &FirstI);
330330 DFSanFunction &DFSF;
331331 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
332332
333 const DataLayout &getDataLayout() const {
334 return DFSF.F->getParent()->getDataLayout();
335 }
336
333337 void visitOperandShadowInst(Instruction &I);
334338
335339 void visitBinaryOperator(BinaryOperator &BO);
14811485 auto *LabelVATy = ArrayType::get(DFSF.DFS.ShadowTy,
14821486 CS.arg_size() - FT->getNumParams());
14831487 auto *LabelVAAlloca = new AllocaInst(
1484 LabelVATy, "labelva", &DFSF.F->getEntryBlock().front());
1488 LabelVATy, getDataLayout().getAllocaAddrSpace(),
1489 "labelva", &DFSF.F->getEntryBlock().front());
14851490
14861491 for (unsigned n = 0; i != CS.arg_end(); ++i, ++n) {
14871492 auto LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, n);
14941499 if (!FT->getReturnType()->isVoidTy()) {
14951500 if (!DFSF.LabelReturnAlloca) {
14961501 DFSF.LabelReturnAlloca =
1497 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1498 &DFSF.F->getEntryBlock().front());
1502 new AllocaInst(DFSF.DFS.ShadowTy,
1503 getDataLayout().getAllocaAddrSpace(),
1504 "labelreturn", &DFSF.F->getEntryBlock().front());
14991505 }
15001506 Args.push_back(DFSF.LabelReturnAlloca);
15011507 }
15741580 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
15751581 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
15761582 AllocaInst *VarArgShadow =
1577 new AllocaInst(VarArgArrayTy, "", &DFSF.F->getEntryBlock().front());
1583 new AllocaInst(VarArgArrayTy, getDataLayout().getAllocaAddrSpace(),
1584 "", &DFSF.F->getEntryBlock().front());
15781585 Args.push_back(IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, 0));
15791586 for (unsigned n = 0; i != e; ++i, ++n) {
15801587 IRB.CreateStore(
16181618
16191619 // Emit alloca for "LiveValue" and record it in "allocaMap" and
16201620 // "PromotableAllocas"
1621 const DataLayout &DL = F.getParent()->getDataLayout();
16211622 auto emitAllocaFor = [&](Value *LiveValue) {
1622 AllocaInst *Alloca = new AllocaInst(LiveValue->getType(), "",
1623 AllocaInst *Alloca = new AllocaInst(LiveValue->getType(),
1624 DL.getAllocaAddrSpace(), "",
16231625 F.getEntryBlock().getFirstNonPHI());
16241626 AllocaMap[LiveValue] = Alloca;
16251627 PromotableAllocas.push_back(Alloca);
22942294 #endif
22952295
22962296 return getAdjustedPtr(IRB, DL, &NewAI,
2297 APInt(DL.getPointerSizeInBits(), Offset), PointerTy,
2297 APInt(DL.getPointerTypeSizeInBits(PointerTy), Offset),
2298 PointerTy,
22982299 #ifndef NDEBUG
22992300 Twine(OldName) + "."
23002301 #else
23682369 DEBUG(dbgs() << " original: " << LI << "\n");
23692370 Value *OldOp = LI.getOperand(0);
23702371 assert(OldOp == OldPtr);
2372
2373 unsigned AS = LI.getPointerAddressSpace();
23712374
23722375 Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8)
23732376 : LI.getType();
24052408 "endian_shift");
24062409 }
24072410 } else {
2408 Type *LTy = TargetTy->getPointerTo();
2411 Type *LTy = TargetTy->getPointerTo(AS);
24092412 LoadInst *NewLI = IRB.CreateAlignedLoad(getNewAllocaSlicePtr(IRB, LTy),
24102413 getSliceAlign(TargetTy),
24112414 LI.isVolatile(), LI.getName());
24332436 // the computed value, and then replace the placeholder with LI, leaving
24342437 // LI only used for this computation.
24352438 Value *Placeholder =
2436 new LoadInst(UndefValue::get(LI.getType()->getPointerTo()));
2439 new LoadInst(UndefValue::get(LI.getType()->getPointerTo(AS)));
24372440 V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset,
24382441 "insert");
24392442 LI.replaceAllUsesWith(V);
25462549 NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(),
25472550 SI.isVolatile());
25482551 } else {
2549 Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo());
2552 unsigned AS = SI.getPointerAddressSpace();
2553 Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo(AS));
25502554 NewSI = IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(V->getType()),
25512555 SI.isVolatile());
25522556 }
38613865 if (Alignment <= DL.getABITypeAlignment(SliceTy))
38623866 Alignment = 0;
38633867 NewAI = new AllocaInst(
3864 SliceTy, nullptr, Alignment,
3868 SliceTy, AI.getType()->getAddressSpace(), nullptr, Alignment,
38653869 AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI);
38663870 ++NumNewAllocas;
38673871 }
439439 // Emit a call to the new function, passing in: *pointer to struct (if
440440 // aggregating parameters), or plan inputs and allocated memory for outputs
441441 std::vector params, StructValues, ReloadOutputs, Reloads;
442
443 LLVMContext &Context = newFunction->getContext();
442
443 Module *M = newFunction->getParent();
444 LLVMContext &Context = M->getContext();
445 const DataLayout &DL = M->getDataLayout();
444446
445447 // Add inputs as params, or to be filled into the struct
446448 for (Value *input : inputs)
455457 StructValues.push_back(output);
456458 } else {
457459 AllocaInst *alloca =
458 new AllocaInst(output->getType(), nullptr, output->getName() + ".loc",
459 &codeReplacer->getParent()->front().front());
460 new AllocaInst(output->getType(), DL.getAllocaAddrSpace(),
461 nullptr, output->getName() + ".loc",
462 &codeReplacer->getParent()->front().front());
460463 ReloadOutputs.push_back(alloca);
461464 params.push_back(alloca);
462465 }
472475
473476 // Allocate a struct at the beginning of this function
474477 StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
475 Struct = new AllocaInst(StructArgTy, nullptr, "structArg",
478 Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
479 "structArg",
476480 &codeReplacer->getParent()->front().front());
477481 params.push_back(Struct);
478482
2727 return nullptr;
2828 }
2929
30 Function *F = I.getParent()->getParent();
31 const DataLayout &DL = F->getParent()->getDataLayout();
32
3033 // Create a stack slot to hold the value.
3134 AllocaInst *Slot;
3235 if (AllocaPoint) {
33 Slot = new AllocaInst(I.getType(), nullptr,
36 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
3437 I.getName()+".reg2mem", AllocaPoint);
3538 } else {
36 Function *F = I.getParent()->getParent();
37 Slot = new AllocaInst(I.getType(), nullptr, I.getName() + ".reg2mem",
38 &F->getEntryBlock().front());
39 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
40 I.getName() + ".reg2mem", &F->getEntryBlock().front());
3941 }
4042
4143 // We cannot demote invoke instructions to the stack if their normal edge
109111 return nullptr;
110112 }
111113
114 const DataLayout &DL = P->getModule()->getDataLayout();
115
112116 // Create a stack slot to hold the value.
113117 AllocaInst *Slot;
114118 if (AllocaPoint) {
115 Slot = new AllocaInst(P->getType(), nullptr,
119 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
116120 P->getName()+".reg2mem", AllocaPoint);
117121 } else {
118122 Function *F = P->getParent()->getParent();
119 Slot = new AllocaInst(P->getType(), nullptr, P->getName() + ".reg2mem",
123 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
124 P->getName() + ".reg2mem",
120125 &F->getEntryBlock().front());
121126 }
122127
12241224 Type *AggTy = ArgTy->getElementType();
12251225
12261226 Function *Caller = TheCall->getFunction();
1227 const DataLayout &DL = Caller->getParent()->getDataLayout();
12271228
12281229 // If the called function is readonly, then it could not mutate the caller's
12291230 // copy of the byval'd memory. In this case, it is safe to elide the copy and
12371238
12381239 AssumptionCache *AC =
12391240 IFI.GetAssumptionCache ? &(*IFI.GetAssumptionCache)(*Caller) : nullptr;
1240 const DataLayout &DL = Caller->getParent()->getDataLayout();
12411241
12421242 // If the pointer is already known to be sufficiently aligned, or if we can
12431243 // round it up to a larger alignment, then we don't need a temporary.
12441244 if (getOrEnforceKnownAlignment(Arg, ByValAlignment, DL, TheCall, AC) >=
12451245 ByValAlignment)
12461246 return Arg;
1247
1247
12481248 // Otherwise, we have to make a memcpy to get a safe alignment. This is bad
12491249 // for code quality, but rarely happens and is required for correctness.
12501250 }
12511251
12521252 // Create the alloca. If we have DataLayout, use nice alignment.
1253 unsigned Align =
1254 Caller->getParent()->getDataLayout().getPrefTypeAlignment(AggTy);
1253 unsigned Align = DL.getPrefTypeAlignment(AggTy);
12551254
12561255 // If the byval had an alignment specified, we *must* use at least that
12571256 // alignment, as it is required by the byval argument (and uses of the
12581257 // pointer inside the callee).
12591258 Align = std::max(Align, ByValAlignment);
1260
1261 Value *NewAlloca = new AllocaInst(AggTy, nullptr, Align, Arg->getName(),
1259
1260 Value *NewAlloca = new AllocaInst(AggTy, DL.getAllocaAddrSpace(),
1261 nullptr, Align, Arg->getName(),
12621262 &*Caller->begin()->begin());
12631263 IFI.StaticAllocas.push_back(cast(NewAlloca));
1264
1264
12651265 // Uses of the argument in the function should use our new alloca
12661266 // instead.
12671267 return NewAlloca;
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A1"
3
4 ; CHECK: :8:3: error: expected metadata after comma
5 define void @use_alloca() {
6 %alloca = alloca i32, addrspace(1),
7 ret void
8 }
9
10 !0 = !{}
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A1"
3
4 ; addrspace and align in wrong order
5 ; CHECK: :8:39: error: expected metadata after comma
6 define void @use_alloca() {
7 %alloca = alloca i32, addrspace(1), align 4
8 ret void
9 }
10
11 !0 = !{}
0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
1
2 target datalayout = "A0"
3 ; CHECK: target datalayout = "A0"
4
5
6 ; CHECK: %alloca_scalar_no_align = alloca i32
7 ; CHECK-NEXT: %alloca_scalar_align4 = alloca i32, align 4
8 ; CHECK-NEXT: %alloca_scalar_no_align_metadata = alloca i32, !foo !0
9 ; CHECK-NEXT: %alloca_scalar_align4_metadata = alloca i32, align 4, !foo !0
10 ; CHECK-NEXT: %alloca_inalloca_scalar_no_align = alloca inalloca i32
11 ; CHECK-NEXT: %alloca_inalloca_scalar_align4_metadata = alloca inalloca i32, align 4, !foo !0
12 define void @use_alloca() {
13 %alloca_scalar_no_align = alloca i32, addrspace(0)
14 %alloca_scalar_align4 = alloca i32, align 4, addrspace(0)
15 %alloca_scalar_no_align_metadata = alloca i32, addrspace(0), !foo !0
16 %alloca_scalar_align4_metadata = alloca i32, align 4, addrspace(0), !foo !0
17 %alloca_inalloca_scalar_no_align = alloca inalloca i32, addrspace(0)
18 %alloca_inalloca_scalar_align4_metadata = alloca inalloca i32, align 4, addrspace(0), !foo !0
19
20 ret void
21 }
22
23 !0 = !{}
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A1"
3
4 ; CHECK: :7:41: error: address space must match datalayout
5 define void @use_alloca() {
6 %alloca_scalar_no_align = alloca i32, addrspace(2)
7 ret void
8 }
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A1"
3
4 ; CHECK: :7:50: error: address space must match datalayout
5 define void @use_alloca() {
6 %alloca_scalar_no_align = alloca i32, align 4, addrspace(2)
7 ret void
8 }
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A1"
3
4 ; CHECK: :7:50: error: address space must match datalayout
5 define void @use_alloca() {
6 %alloca_scalar_no_align = alloca i32, align 4, addrspace(2), !foo !0
7 ret void
8 }
9
10 !0 = !{}
0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
1
2 target datalayout = "A1"
3 ; CHECK: target datalayout = "A1"
4
5 ; CHECK: %alloca_scalar_no_align = alloca i32, addrspace(1)
6 ; CHECK-NEXT: %alloca_scalar_align4 = alloca i32, align 4, addrspace(1)
7 ; CHECK-NEXT: %alloca_scalar_no_align_metadata = alloca i32, addrspace(1), !foo !0
8 ; CHECK-NEXT: %alloca_scalar_align4_metadata = alloca i32, align 4, addrspace(1), !foo !0
9 ; CHECK-NEXT: %alloca_inalloca_scalar_no_align = alloca inalloca i32, addrspace(1)
10 ; CHECK-NEXT: %alloca_inalloca_scalar_align4_metadata = alloca inalloca i32, align 4, addrspace(1), !foo !0
11 define void @use_alloca() {
12 %alloca_scalar_no_align = alloca i32, addrspace(1)
13 %alloca_scalar_align4 = alloca i32, align 4, addrspace(1)
14 %alloca_scalar_no_align_metadata = alloca i32, addrspace(1), !foo !0
15 %alloca_scalar_align4_metadata = alloca i32, align 4, addrspace(1), !foo !0
16 %alloca_inalloca_scalar_no_align = alloca inalloca i32, addrspace(1)
17 %alloca_inalloca_scalar_align4_metadata = alloca inalloca i32, align 4, addrspace(1), !foo !0
18
19 ret void
20 }
21
22 !0 = !{}
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 target datalayout = "A16777216"
3 ; CHECK: Invalid address space, must be a 24bit integer
0 ; RUN: opt < %s -sroa -S | FileCheck %s
1 target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64-A2"
2
3 declare void @llvm.memcpy.p2i8.p2i8.i32(i8 addrspace(2)* nocapture, i8 addrspace(2)* nocapture readonly, i32, i32, i1)
4 declare void @llvm.memcpy.p1i8.p2i8.i32(i8 addrspace(1)* nocapture, i8 addrspace(2)* nocapture readonly, i32, i32, i1)
5 declare void @llvm.memcpy.p2i8.p1i8.i32(i8 addrspace(2)* nocapture, i8 addrspace(1)* nocapture readonly, i32, i32, i1)
6 declare void @llvm.memcpy.p1i8.p1i8.i32(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture readonly, i32, i32, i1)
7
8
9
10 ; CHECK-LABEL: @test_address_space_1_1(
11 ; CHECK: load <2 x i64>, <2 x i64> addrspace(1)* %a, align 2
12 ; CHECK: store <2 x i64> {{.*}}, <2 x i64> addrspace(1)* {{.*}}, align 2
13 ; CHECK: ret void
14 define void @test_address_space_1_1(<2 x i64> addrspace(1)* %a, i16 addrspace(1)* %b) {
15 %aa = alloca <2 x i64>, align 16, addrspace(2)
16 %aptr = bitcast <2 x i64> addrspace(1)* %a to i8 addrspace(1)*
17 %aaptr = bitcast <2 x i64> addrspace(2)* %aa to i8 addrspace(2)*
18 call void @llvm.memcpy.p2i8.p1i8.i32(i8 addrspace(2)* %aaptr, i8 addrspace(1)* %aptr, i32 16, i32 2, i1 false)
19 %bptr = bitcast i16 addrspace(1)* %b to i8 addrspace(1)*
20 call void @llvm.memcpy.p1i8.p2i8.i32(i8 addrspace(1)* %bptr, i8 addrspace(2)* %aaptr, i32 16, i32 2, i1 false)
21 ret void
22 }
23
24 ; CHECK-LABEL: @test_address_space_1_0(
25 ; CHECK: load <2 x i64>, <2 x i64> addrspace(1)* %a, align 2
26 ; CHECK: store <2 x i64> {{.*}}, <2 x i64> addrspace(2)* {{.*}}, align 2
27 ; CHECK: ret void
28 define void @test_address_space_1_0(<2 x i64> addrspace(1)* %a, i16 addrspace(2)* %b) {
29 %aa = alloca <2 x i64>, align 16, addrspace(2)
30 %aptr = bitcast <2 x i64> addrspace(1)* %a to i8 addrspace(1)*
31 %aaptr = bitcast <2 x i64> addrspace(2)* %aa to i8 addrspace(2)*
32 call void @llvm.memcpy.p2i8.p1i8.i32(i8 addrspace(2)* %aaptr, i8 addrspace(1)* %aptr, i32 16, i32 2, i1 false)
33 %bptr = bitcast i16 addrspace(2)* %b to i8 addrspace(2)*
34 call void @llvm.memcpy.p2i8.p2i8.i32(i8 addrspace(2)* %bptr, i8 addrspace(2)* %aaptr, i32 16, i32 2, i1 false)
35 ret void
36 }
37
38 ; CHECK-LABEL: @test_address_space_0_1(
39 ; CHECK: load <2 x i64>, <2 x i64> addrspace(2)* %a, align 2
40 ; CHECK: store <2 x i64> {{.*}}, <2 x i64> addrspace(1)* {{.*}}, align 2
41 ; CHECK: ret void
42 define void @test_address_space_0_1(<2 x i64> addrspace(2)* %a, i16 addrspace(1)* %b) {
43 %aa = alloca <2 x i64>, align 16, addrspace(2)
44 %aptr = bitcast <2 x i64> addrspace(2)* %a to i8 addrspace(2)*
45 %aaptr = bitcast <2 x i64> addrspace(2)* %aa to i8 addrspace(2)*
46 call void @llvm.memcpy.p2i8.p2i8.i32(i8 addrspace(2)* %aaptr, i8 addrspace(2)* %aptr, i32 16, i32 2, i1 false)
47 %bptr = bitcast i16 addrspace(1)* %b to i8 addrspace(1)*
48 call void @llvm.memcpy.p1i8.p2i8.i32(i8 addrspace(1)* %bptr, i8 addrspace(2)* %aaptr, i32 16, i32 2, i1 false)
49 ret void
50 }
51
52 %struct.struct_test_27.0.13 = type { i32, float, i64, i8, [4 x i32] }
53
54 ; CHECK-LABEL: @copy_struct(
55 ; CHECK-NOT: memcpy
56 define void @copy_struct([5 x i64] %in.coerce) {
57 for.end:
58 %in = alloca %struct.struct_test_27.0.13, align 8, addrspace(2)
59 %0 = bitcast %struct.struct_test_27.0.13 addrspace(2)* %in to [5 x i64] addrspace(2)*
60 store [5 x i64] %in.coerce, [5 x i64] addrspace(2)* %0, align 8
61 %scevgep9 = getelementptr %struct.struct_test_27.0.13, %struct.struct_test_27.0.13 addrspace(2)* %in, i32 0, i32 4, i32 0
62 %scevgep910 = bitcast i32 addrspace(2)* %scevgep9 to i8 addrspace(2)*
63 call void @llvm.memcpy.p1i8.p2i8.i32(i8 addrspace(1)* undef, i8 addrspace(2)* %scevgep910, i32 16, i32 4, i1 false)
64 ret void
65 }
66
67 %union.anon = type { i32* }
68
69 @g = common global i32 0, align 4
70 @l = common addrspace(3) global i32 0, align 4
71
72 ; Make sure an illegal bitcast isn't introduced
73 ; CHECK-LABEL: @pr27557(
74 ; CHECK: %[[CAST:.*]] = bitcast i32* addrspace(2)* {{.*}} to i32 addrspace(3)* addrspace(2)*
75 ; CHECK: store i32 addrspace(3)* @l, i32 addrspace(3)* addrspace(2)* %[[CAST]]
76 define void @pr27557() {
77 %1 = alloca %union.anon, align 8, addrspace(2)
78 %2 = bitcast %union.anon addrspace(2)* %1 to i32* addrspace(2)*
79 store i32* @g, i32* addrspace(2)* %2, align 8
80 %3 = bitcast %union.anon addrspace(2)* %1 to i32 addrspace(3)* addrspace(2)*
81 store i32 addrspace(3)* @l, i32 addrspace(3)* addrspace(2)* %3, align 8
82 ret void
83 }
423423
424424 void Act() override {
425425 Type *Tp = pickType();
426 PT->push_back(new AllocaInst(Tp, "A", BB->getFirstNonPHI()));
426 const DataLayout &DL = BB->getModule()->getDataLayout();
427 PT->push_back(new AllocaInst(Tp, DL.getAllocaAddrSpace(),
428 "A", BB->getFirstNonPHI()));
427429 }
428430 };
429431
305305 // %bitcast2 = bitcast i8* %select to i32*
306306 // br i1 undef, label %loop, label %exit
307307
308 const DataLayout &DL = F->getParent()->getDataLayout();
308309 BranchInst *Br = BranchInst::Create(
309310 LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
310 AllocaInst *Alloca = new AllocaInst(I32Ty, "alloca", Br);
311 AllocaInst *Alloca = new AllocaInst(I32Ty, DL.getAllocaAddrSpace(),
312 "alloca", Br);
311313 ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
312314 GetElementPtrInst *Gep0 =
313315 GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);