@@ -124,6 +124,7 @@ class GetElementPtrInst;
124124class CastInst ;
125125class PtrToIntInst ;
126126class BitCastInst ;
127+ class AllocaInst ;
127128
128129// / Iterator for the `Use` edges of a User's operands.
129130// / \Returns the operand `Use` when dereferenced.
@@ -240,6 +241,7 @@ class Value {
240241 friend class InvokeInst ; // For getting `Val`.
241242 friend class CallBrInst ; // For getting `Val`.
242243 friend class GetElementPtrInst ; // For getting `Val`.
244+ friend class AllocaInst ; // For getting `Val`.
243245 friend class CastInst ; // For getting `Val`.
244246 friend class PHINode ; // For getting `Val`.
245247
@@ -633,6 +635,7 @@ class Instruction : public sandboxir::User {
633635 friend class InvokeInst ; // For getTopmostLLVMInstruction().
634636 friend class CallBrInst ; // For getTopmostLLVMInstruction().
635637 friend class GetElementPtrInst ; // For getTopmostLLVMInstruction().
638+ friend class AllocaInst ; // For getTopmostLLVMInstruction().
636639 friend class CastInst ; // For getTopmostLLVMInstruction().
637640 friend class PHINode ; // For getTopmostLLVMInstruction().
638641
@@ -1393,6 +1396,103 @@ class GetElementPtrInst final : public Instruction {
13931396#endif
13941397};
13951398
1399+ class AllocaInst final : public UnaryInstruction {
1400+ Use getOperandUseInternal (unsigned OpIdx, bool Verify) const final {
1401+ return getOperandUseDefault (OpIdx, Verify);
1402+ }
1403+ SmallVector<llvm::Instruction *, 1 > getLLVMInstrs () const final {
1404+ return {cast<llvm::Instruction>(Val)};
1405+ }
1406+
1407+ AllocaInst (llvm::AllocaInst *AI, Context &Ctx)
1408+ : UnaryInstruction(ClassID::Alloca, Instruction::Opcode::Alloca, AI,
1409+ Ctx) {}
1410+ friend class Context ; // For constructor.
1411+
1412+ public:
1413+ static AllocaInst *create (Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
1414+ BasicBlock *WhereBB, Context &Ctx,
1415+ Value *ArraySize = nullptr , const Twine &Name = " " );
1416+ static AllocaInst *create (Type *Ty, unsigned AddrSpace,
1417+ Instruction *InsertBefore, Context &Ctx,
1418+ Value *ArraySize = nullptr , const Twine &Name = " " );
1419+ static AllocaInst *create (Type *Ty, unsigned AddrSpace,
1420+ BasicBlock *InsertAtEnd, Context &Ctx,
1421+ Value *ArraySize = nullptr , const Twine &Name = " " );
1422+
1423+ unsigned getUseOperandNo (const Use &Use) const final {
1424+ return getUseOperandNoDefault (Use);
1425+ }
1426+ unsigned getNumOfIRInstrs () const final { return 1u ; }
1427+
1428+ // / Return true if there is an allocation size parameter to the allocation
1429+ // / instruction that is not 1.
1430+ bool isArrayAllocation () const {
1431+ return cast<llvm::AllocaInst>(Val)->isArrayAllocation ();
1432+ }
1433+ // / Get the number of elements allocated. For a simple allocation of a single
1434+ // / element, this will return a constant 1 value.
1435+ Value *getArraySize ();
1436+ const Value *getArraySize () const {
1437+ return const_cast <AllocaInst *>(this )->getArraySize ();
1438+ }
1439+ // / Overload to return most specific pointer type.
1440+ PointerType *getType () const {
1441+ return cast<llvm::AllocaInst>(Val)->getType ();
1442+ }
1443+ // / Return the address space for the allocation.
1444+ unsigned getAddressSpace () const {
1445+ return cast<llvm::AllocaInst>(Val)->getAddressSpace ();
1446+ }
1447+ // / Get allocation size in bytes. Returns std::nullopt if size can't be
1448+ // / determined, e.g. in case of a VLA.
1449+ std::optional<TypeSize> getAllocationSize (const DataLayout &DL) const {
1450+ return cast<llvm::AllocaInst>(Val)->getAllocationSize (DL);
1451+ }
1452+ // / Get allocation size in bits. Returns std::nullopt if size can't be
1453+ // / determined, e.g. in case of a VLA.
1454+ std::optional<TypeSize> getAllocationSizeInBits (const DataLayout &DL) const {
1455+ return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits (DL);
1456+ }
1457+ // / Return the type that is being allocated by the instruction.
1458+ Type *getAllocatedType () const {
1459+ return cast<llvm::AllocaInst>(Val)->getAllocatedType ();
1460+ }
1461+ // / for use only in special circumstances that need to generically
1462+ // / transform a whole instruction (eg: IR linking and vectorization).
1463+ void setAllocatedType (Type *Ty);
1464+ // / Return the alignment of the memory that is being allocated by the
1465+ // / instruction.
1466+ Align getAlign () const { return cast<llvm::AllocaInst>(Val)->getAlign (); }
1467+ void setAlignment (Align Align);
1468+ // / Return true if this alloca is in the entry block of the function and is a
1469+ // / constant size. If so, the code generator will fold it into the
1470+ // / prolog/epilog code, so it is basically free.
1471+ bool isStaticAlloca () const {
1472+ return cast<llvm::AllocaInst>(Val)->isStaticAlloca ();
1473+ }
1474+ // / Return true if this alloca is used as an inalloca argument to a call. Such
1475+ // / allocas are never considered static even if they are in the entry block.
1476+ bool isUsedWithInAlloca () const {
1477+ return cast<llvm::AllocaInst>(Val)->isUsedWithInAlloca ();
1478+ }
1479+ // / Specify whether this alloca is used to represent the arguments to a call.
1480+ void setUsedWithInAlloca (bool V);
1481+
1482+ static bool classof (const Value *From) {
1483+ if (auto *I = dyn_cast<Instruction>(From))
1484+ return I->getSubclassID () == Instruction::ClassID::Alloca;
1485+ return false ;
1486+ }
1487+ #ifndef NDEBUG
1488+ void verify () const final {
1489+ assert (isa<llvm::AllocaInst>(Val) && " Expected AllocaInst!" );
1490+ }
1491+ void dump (raw_ostream &OS) const override ;
1492+ LLVM_DUMP_METHOD void dump () const override ;
1493+ #endif
1494+ };
1495+
13961496class CastInst : public UnaryInstruction {
13971497 static Opcode getCastOpcode (llvm::Instruction::CastOps CastOp) {
13981498 switch (CastOp) {
@@ -1726,6 +1826,8 @@ class Context {
17261826 friend CallBrInst; // For createCallBrInst()
17271827 GetElementPtrInst *createGetElementPtrInst (llvm::GetElementPtrInst *I);
17281828 friend GetElementPtrInst; // For createGetElementPtrInst()
1829+ AllocaInst *createAllocaInst (llvm::AllocaInst *I);
1830+ friend AllocaInst; // For createAllocaInst()
17291831 CastInst *createCastInst (llvm::CastInst *I);
17301832 friend CastInst; // For createCastInst()
17311833 PHINode *createPHINode (llvm::PHINode *I);
0 commit comments