OLD | NEW |
(Empty) | |
| 1 //===- ExpandByVal.cpp - Expand out use of "byval" and "sret" attributes---===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This pass expands out by-value passing of structs as arguments and |
| 11 // return values. In LLVM IR terms, it expands out the "byval" and |
| 12 // "sret" function argument attributes. |
| 13 // |
| 14 // The semantics of the "byval" attribute are that the callee function |
| 15 // gets a private copy of the pointed-to argument that it is allowed |
| 16 // to modify. In implementing this, we have a choice between making |
| 17 // the caller responsible for making the copy or making the callee |
| 18 // responsible for making the copy. We choose the former, because |
| 19 // this matches how the normal native calling conventions work, and |
| 20 // because it often allows the caller to write struct contents |
| 21 // directly into the stack slot that it passes the callee, without an |
| 22 // additional copy. |
| 23 // |
| 24 // Note that this pass does not attempt to modify functions that pass |
| 25 // structs by value without using "byval" or "sret", such as: |
| 26 // |
| 27 // define %struct.X @func() ; struct return |
| 28 // define void @func(%struct.X %arg) ; struct arg |
| 29 // |
| 30 // The pass only handles functions such as: |
| 31 // |
| 32 // define void @func(%struct.X* sret %result_buffer) ; struct return |
| 33 // define void @func(%struct.X* byval %ptr_to_arg) ; struct arg |
| 34 // |
| 35 // This is because PNaCl Clang generates the latter and not the former. |
| 36 // |
| 37 //===----------------------------------------------------------------------===// |
| 38 |
| 39 #include "llvm/IR/Attributes.h" |
| 40 #include "llvm/IR/DataLayout.h" |
| 41 #include "llvm/IR/Function.h" |
| 42 #include "llvm/IR/IRBuilder.h" |
| 43 #include "llvm/IR/Instructions.h" |
| 44 #include "llvm/IR/Module.h" |
| 45 #include "llvm/Pass.h" |
| 46 #include "llvm/Transforms/NaCl.h" |
| 47 |
| 48 using namespace llvm; |
| 49 |
| 50 namespace { |
| 51 // This is a ModulePass so that it can strip attributes from |
| 52 // declared functions as well as defined functions. |
| 53 class ExpandByVal : public ModulePass { |
| 54 public: |
| 55 static char ID; // Pass identification, replacement for typeid |
| 56 ExpandByVal() : ModulePass(ID) { |
| 57 initializeExpandByValPass(*PassRegistry::getPassRegistry()); |
| 58 } |
| 59 |
| 60 virtual bool runOnModule(Module &M); |
| 61 }; |
| 62 } |
| 63 |
| 64 char ExpandByVal::ID = 0; |
| 65 INITIALIZE_PASS(ExpandByVal, "expand-byval", |
| 66 "Expand out by-value passing of structs", |
| 67 false, false) |
| 68 |
| 69 // removeAttribute() currently does not work on Attribute::Alignment |
| 70 // (it fails with an assertion error), so we have to take a more |
| 71 // convoluted route to removing this attribute by recreating the |
| 72 // AttributeSet. |
| 73 AttributeSet RemoveAttrs(LLVMContext &Context, AttributeSet Attrs) { |
| 74 SmallVector<AttributeSet, 8> AttrList; |
| 75 for (unsigned Slot = 0; Slot < Attrs.getNumSlots(); ++Slot) { |
| 76 unsigned Index = Attrs.getSlotIndex(Slot); |
| 77 AttrBuilder AB; |
| 78 for (AttributeSet::iterator Attr = Attrs.begin(Slot), E = Attrs.end(Slot); |
| 79 Attr != E; ++Attr) { |
| 80 if (Attr->isEnumAttribute() && |
| 81 Attr->getKindAsEnum() != Attribute::ByVal && |
| 82 Attr->getKindAsEnum() != Attribute::StructRet) { |
| 83 AB.addAttribute(*Attr); |
| 84 } |
| 85 // IR semantics require that ByVal implies NoAlias. However, IR |
| 86 // semantics do not require StructRet to imply NoAlias. For |
| 87 // example, a global variable address can be passed as a |
| 88 // StructRet argument, although Clang does not do so and Clang |
| 89 // explicitly adds NoAlias to StructRet arguments. |
| 90 if (Attr->isEnumAttribute() && |
| 91 Attr->getKindAsEnum() == Attribute::ByVal) { |
| 92 AB.addAttribute(Attribute::get(Context, Attribute::NoAlias)); |
| 93 } |
| 94 } |
| 95 AttrList.push_back(AttributeSet::get(Context, Index, AB)); |
| 96 } |
| 97 return AttributeSet::get(Context, AttrList); |
| 98 } |
| 99 |
| 100 // ExpandCall() can take a CallInst or an InvokeInst. It returns |
| 101 // whether the instruction was modified. |
| 102 template <class InstType> |
| 103 static bool ExpandCall(DataLayout *DL, InstType *Call) { |
| 104 bool Modify = false; |
| 105 AttributeSet Attrs = Call->getAttributes(); |
| 106 for (unsigned ArgIdx = 0; ArgIdx < Call->getNumArgOperands(); ++ArgIdx) { |
| 107 unsigned AttrIdx = ArgIdx + 1; |
| 108 |
| 109 if (Attrs.hasAttribute(AttrIdx, Attribute::StructRet)) |
| 110 Modify = true; |
| 111 |
| 112 if (Attrs.hasAttribute(AttrIdx, Attribute::ByVal)) { |
| 113 Modify = true; |
| 114 |
| 115 Value *ArgPtr = Call->getArgOperand(ArgIdx); |
| 116 Type *ArgType = ArgPtr->getType()->getPointerElementType(); |
| 117 ConstantInt *ArgSize = ConstantInt::get( |
| 118 Call->getContext(), APInt(64, DL->getTypeStoreSize(ArgType))); |
| 119 // In principle, using the alignment from the argument attribute |
| 120 // should be enough. However, Clang is not emitting this |
| 121 // attribute for PNaCl. LLVM alloca instructions do not use the |
| 122 // ABI alignment of the type, so this must be specified |
| 123 // explicitly. |
| 124 // See https://code.google.com/p/nativeclient/issues/detail?id=3403 |
| 125 // |
| 126 // Note that the parameter may have no alignment, but we have |
| 127 // more useful information from the type which we can use here |
| 128 // -- 0 in the parameter means no alignment is specified there, |
| 129 // so it has default alignment, but in memcpy 0 means |
| 130 // pessimistic alignment, the same as 1. |
| 131 unsigned Alignment = |
| 132 std::max(Attrs.getParamAlignment(AttrIdx), |
| 133 DL->getABITypeAlignment(ArgType)); |
| 134 |
| 135 // Make a copy of the byval argument. |
| 136 Instruction *CopyBuf = new AllocaInst(ArgType, 0, Alignment, |
| 137 ArgPtr->getName() + ".byval_copy"); |
| 138 Function *Func = Call->getParent()->getParent(); |
| 139 Func->getEntryBlock().getInstList().push_front(CopyBuf); |
| 140 IRBuilder<> Builder(Call); |
| 141 Builder.CreateLifetimeStart(CopyBuf, ArgSize); |
| 142 // Using the argument's alignment attribute for the memcpy |
| 143 // should be OK because the LLVM Language Reference says that |
| 144 // the alignment attribute specifies "the alignment of the stack |
| 145 // slot to form and the known alignment of the pointer specified |
| 146 // to the call site". |
| 147 Instruction *MemCpy = Builder.CreateMemCpy(CopyBuf, ArgPtr, ArgSize, |
| 148 Alignment); |
| 149 MemCpy->setDebugLoc(Call->getDebugLoc()); |
| 150 |
| 151 Call->setArgOperand(ArgIdx, CopyBuf); |
| 152 |
| 153 // Mark the argument copy as unused using llvm.lifetime.end. |
| 154 if (isa<CallInst>(Call)) { |
| 155 BasicBlock::iterator It = BasicBlock::iterator(Call); |
| 156 Builder.SetInsertPoint(++It); |
| 157 Builder.CreateLifetimeEnd(CopyBuf, ArgSize); |
| 158 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Call)) { |
| 159 Builder.SetInsertPoint(Invoke->getNormalDest()->getFirstInsertionPt()); |
| 160 Builder.CreateLifetimeEnd(CopyBuf, ArgSize); |
| 161 Builder.SetInsertPoint(Invoke->getUnwindDest()->getFirstInsertionPt()); |
| 162 Builder.CreateLifetimeEnd(CopyBuf, ArgSize); |
| 163 } |
| 164 } |
| 165 } |
| 166 if (Modify) { |
| 167 Call->setAttributes(RemoveAttrs(Call->getContext(), Attrs)); |
| 168 |
| 169 if (CallInst *CI = dyn_cast<CallInst>(Call)) { |
| 170 // This is no longer a tail call because the callee references |
| 171 // memory alloca'd by the caller. |
| 172 CI->setTailCall(false); |
| 173 } |
| 174 } |
| 175 return Modify; |
| 176 } |
| 177 |
| 178 bool ExpandByVal::runOnModule(Module &M) { |
| 179 bool Modified = false; |
| 180 DataLayout DL(&M); |
| 181 |
| 182 for (Module::iterator Func = M.begin(), E = M.end(); Func != E; ++Func) { |
| 183 AttributeSet NewAttrs = RemoveAttrs(Func->getContext(), |
| 184 Func->getAttributes()); |
| 185 Modified |= (NewAttrs != Func->getAttributes()); |
| 186 Func->setAttributes(NewAttrs); |
| 187 |
| 188 for (Function::iterator BB = Func->begin(), E = Func->end(); |
| 189 BB != E; ++BB) { |
| 190 for (BasicBlock::iterator Inst = BB->begin(), E = BB->end(); |
| 191 Inst != E; ++Inst) { |
| 192 if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 193 Modified |= ExpandCall(&DL, Call); |
| 194 } else if (InvokeInst *Call = dyn_cast<InvokeInst>(Inst)) { |
| 195 Modified |= ExpandCall(&DL, Call); |
| 196 } |
| 197 } |
| 198 } |
| 199 } |
| 200 |
| 201 return Modified; |
| 202 } |
| 203 |
| 204 ModulePass *llvm::createExpandByValPass() { |
| 205 return new ExpandByVal(); |
| 206 } |
OLD | NEW |