llvm.org GIT mirror llvm / a3355ff
Rather than having special rules like "intrinsics cannot throw exceptions", just mark intrinsics with the nounwind attribute. Likewise, mark intrinsics as readnone/readonly and get rid of special aliasing logic (which didn't use anything more than this anyway). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44544 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 11 years ago
22 changed file(s) with 137 addition(s) and 147 deletion(s). Raw diff Collapse all Expand all
157157 return ParamAttrs && ParamAttrs->paramHasAttr(i, attr);
158158 }
159159
160 /// @brief Determine if the function does not access memory.
161 bool doesNotAccessMemory() const {
162 return paramHasAttr(0, ParamAttr::ReadNone);
163 }
164
165 /// @brief Determine if the function does not access or only reads memory.
166 bool onlyReadsMemory() const {
167 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
168 }
169
160170 /// @brief Determine if the function returns a structure.
161171 bool isStructReturn() const {
162172 return paramHasAttr(1, ParamAttr::StructRet);
926926 /// @brief Determine whether the call or the callee has the given attribute.
927927 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
928928
929 /// @brief Determine if the call does not access memory.
930 bool doesNotAccessMemory() const {
931 return paramHasAttr(0, ParamAttr::ReadNone);
932 }
933
934 /// @brief Determine if the call does not access or only reads memory.
935 bool onlyReadsMemory() const {
936 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
937 }
938
939 /// @brief Determine if the call cannot unwind.
940 bool isNoUnwind() const {
941 return paramHasAttr(0, ParamAttr::NoUnwind);
942 }
943
929944 /// @brief Determine if the call returns a structure.
930945 bool isStructReturn() const {
931946 // Be friendly and also check the callee.
17101725 /// @brief Determine whether the call or the callee has the given attribute.
17111726 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
17121727
1728 /// @brief Determine if the call does not access memory.
1729 bool doesNotAccessMemory() const {
1730 return paramHasAttr(0, ParamAttr::ReadNone);
1731 }
1732
1733 /// @brief Determine if the call does not access or only reads memory.
1734 bool onlyReadsMemory() const {
1735 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1736 }
1737
1738 /// @brief Determine if the call cannot unwind.
1739 bool isNoUnwind() const {
1740 return paramHasAttr(0, ParamAttr::NoUnwind);
1741 }
1742
17131743 /// @brief Determine if the call returns a structure.
17141744 bool isStructReturn() const {
17151745 // Be friendly and also check the callee.
2121 class FunctionType;
2222 class Function;
2323 class Module;
24 class ParamAttrsList;
2425
2526 /// Intrinsic Namespace - This namespace contains an enum with a value for
2627 /// every intrinsic/builtin function known by LLVM. These enum values are
4546 ///
4647 const FunctionType *getType(ID id, const Type **Tys = 0, unsigned numTys = 0);
4748
49 /// Intrinsic::getParamAttrs(ID) - Return the attributes for an intrinsic.
50 ///
51 const ParamAttrsList *getParamAttrs(ID id);
52
4853 /// Intrinsic::getDeclaration(M, ID) - Create or insert an LLVM Function
4954 /// declaration for an intrinsic, and return it.
5055 Function *getDeclaration(Module *M, ID id, const Type **Tys = 0,
6666 /// paramHasAttr - whether the call or the callee has the given attribute.
6767 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
6868
69 /// @brief Determine if the call does not access memory.
70 bool doesNotAccessMemory() const;
71
72 /// @brief Determine if the call does not access or only reads memory.
73 bool onlyReadsMemory() const;
74
6975 /// getType - Return the type of the instruction that generated this call site
7076 ///
7177 const Type *getType() const { return I->getType(); }
115115 AliasAnalysis::ModRefBehavior
116116 AliasAnalysis::getModRefBehavior(CallSite CS,
117117 std::vector *Info) {
118 if (CS.paramHasAttr(0, ParamAttr::ReadNone))
118 if (CS.doesNotAccessMemory())
119119 // Can't do better than this.
120120 return DoesNotAccessMemory;
121121 ModRefBehavior MRB = UnknownModRefBehavior;
122122 if (Function *F = CS.getCalledFunction())
123123 MRB = getModRefBehavior(F, CS, Info);
124 if (MRB != DoesNotAccessMemory && CS.paramHasAttr(0, ParamAttr::ReadOnly))
124 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
125125 return OnlyReadsMemory;
126126 return MRB;
127127 }
129129 AliasAnalysis::ModRefBehavior
130130 AliasAnalysis::getModRefBehavior(Function *F,
131131 std::vector *Info) {
132 if (F->paramHasAttr(0, ParamAttr::ReadNone))
132 if (F->doesNotAccessMemory())
133133 // Can't do better than this.
134134 return DoesNotAccessMemory;
135135 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
136 if (MRB != DoesNotAccessMemory && F->paramHasAttr(0, ParamAttr::ReadOnly))
136 if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
137137 return OnlyReadsMemory;
138138 return MRB;
139139 }
2424 #include "llvm/Pass.h"
2525 #include "llvm/Target/TargetData.h"
2626 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringMap.h"
28 #include "llvm/ADT/BitVector.h"
2927 #include "llvm/ADT/STLExtras.h"
3028 #include "llvm/Support/Compiler.h"
3129 #include "llvm/Support/GetElementPtrTypeIterator.h"
114112 /// pointsToConstantMemory - Chase pointers until we find a (constant
115113 /// global) or not.
116114 bool pointsToConstantMemory(const Value *P);
117
118 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
119 std::vector *Info);
120115
121116 private:
122117 // CheckGEPInstructions - Check two GEP instructions with known
809804 return MayAlias;
810805 }
811806
812 static ManagedStatic NoMemoryIntrinsics;
813 static ManagedStatic OnlyReadsMemoryIntrinsics;
814
815 AliasAnalysis::ModRefBehavior
816 BasicAliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
817 std::vector *Info) {
818 if (!F->isDeclaration()) return UnknownModRefBehavior;
819
820 static bool Initialized = false;
821 if (!Initialized) {
822 NoMemoryIntrinsics->resize(Intrinsic::num_intrinsics);
823 OnlyReadsMemoryIntrinsics->resize(Intrinsic::num_intrinsics);
824 #define GET_MODREF_BEHAVIOR
825 #include "llvm/Intrinsics.gen"
826 #undef GET_MODREF_BEHAVIOR
827
828 Initialized = true;
829 }
830
831 // If this is an intrinsic, we can use lookup tables
832 if (unsigned id = F->getIntrinsicID()) {
833 if (NoMemoryIntrinsics->test(id))
834 return DoesNotAccessMemory;
835 if (OnlyReadsMemoryIntrinsics->test(id))
836 return OnlyReadsMemory;
837
838 return UnknownModRefBehavior;
839 }
840
841 return UnknownModRefBehavior;
842 }
843
844807 // Make sure that anything that uses AliasAnalysis pulls in this file...
845808 DEFINING_FILE_FOR(BasicAliasAnalysis)
135135
136136 // If this function is not defined in this translation unit, it could call
137137 // anything.
138 if (F->isDeclaration() && !F->getIntrinsicID())
138 if (F->isDeclaration() && !F->isIntrinsic())
139139 Node->addCalledFunction(CallSite(), CallsExternalNode);
140140
141141 // Loop over all of the users of the function... looking for callers...
24162416 I.isVolatile(), I.getAlignment()));
24172417 }
24182418
2419 /// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot
2420 /// access memory and has no other side effects at all.
2421 static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) {
2422 #define GET_NO_MEMORY_INTRINSICS
2423 #include "llvm/Intrinsics.gen"
2424 #undef GET_NO_MEMORY_INTRINSICS
2425 return false;
2426 }
2427
2428 // IntrinsicOnlyReadsMemory - Return true if the specified intrinsic doesn't
2429 // have any side-effects or if it only reads memory.
2430 static bool IntrinsicOnlyReadsMemory(unsigned IntrinsicID) {
2431 #define GET_SIDE_EFFECT_INFO
2432 #include "llvm/Intrinsics.gen"
2433 #undef GET_SIDE_EFFECT_INFO
2434 return false;
2435 }
2436
24372419 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
24382420 /// node.
24392421 void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
24402422 unsigned Intrinsic) {
2441 bool HasChain = !IntrinsicCannotAccessMemory(Intrinsic);
2442 bool OnlyLoad = HasChain && IntrinsicOnlyReadsMemory(Intrinsic);
2443
2423 bool HasChain = !I.doesNotAccessMemory();
2424 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2425
24442426 // Build the operand list.
24452427 SmallVector Ops;
24462428 if (HasChain) { // If this intrinsic has side-effects, chainify it.
15501550
15511551 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
15521552 // Don't print declarations for intrinsic functions.
1553 if (!I->getIntrinsicID() && I->getName() != "setjmp" &&
1553 if (!I->isIntrinsic() && I->getName() != "setjmp" &&
15541554 I->getName() != "longjmp" && I->getName() != "_setjmp") {
15551555 if (I->hasExternalWeakLinkage())
15561556 Out << "extern ";
15851585 // Functions.
15861586 for (I=ModulePtr->begin(),E=ModulePtr->end(); I!=E; ++I) {
15871587 // Skip intrisics
1588 if (I->getIntrinsicID()) continue;
1588 if (I->isIntrinsic()) continue;
15891589 if (I->isDeclaration()) {
15901590 const Function* F = I;
15911591 std::string Name = getConvModopt(F->getCallingConv())+getValueName(F);
283283 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
284284
285285 if (!F.hasInternalLinkage() &&
286 (!ShouldHackArguments() || F.getIntrinsicID()))
286 (!ShouldHackArguments() || F.isIntrinsic()))
287287 FunctionIntrinsicallyLive = true;
288288 else
289289 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
176176 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
177177 if (CallInst *CI = dyn_cast(II++)) {
178178 if (!CI->getCalledFunction() ||
179 !CI->getCalledFunction()->getIntrinsicID())
179 !CI->getCalledFunction()->isIntrinsic())
180180 NormalCalls.push_back(CI); // Remember all normal function calls.
181181
182182 if (Function *F = CI->getCalledFunction())
134134
135135 // Turn invokes that call 'nounwind' functions into ordinary calls.
136136 if (InvokeInst *II = dyn_cast(BB->getTerminator()))
137 if (II->paramHasAttr(0, ParamAttr::NoUnwind)) {
137 if (II->isNoUnwind()) {
138138 ChangeToCall(II);
139139 Changed = true;
140140 }
6868 if (!isa(I)) continue;
6969 CallInst *CI = cast(I);
7070
71 // If this is an intrinsic function call or an inline asm, don't
71 // If this call cannot unwind or is an inline asm, don't
7272 // convert it to an invoke.
73 if ((CI->getCalledFunction() &&
74 CI->getCalledFunction()->getIntrinsicID()) ||
75 isa(CI->getCalledValue()))
73 if (CI->isNoUnwind() || isa(CI->getCalledValue()))
7674 continue;
77
75
7876 // Convert this function call into an invoke instruction.
7977 // First, split the basic block.
8078 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
1919 using namespace llvm;
2020
2121
22 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
22 static Function* UpgradeIntrinsicFunction1(Function *F) {
2323 assert(F && "Illegal to upgrade a non-existent Function.");
2424
2525 // Get the Function's name.
116116 // upgraded form of the intrinsic. We should perhaps have two separate
117117 // functions for this.
118118 return 0;
119 }
120
121 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
122 Function *Upgraded = UpgradeIntrinsicFunction1(F);
123
124 // Upgrade intrinsic attributes. This does not change the function.
125 if (Upgraded)
126 F = Upgraded;
127 if (unsigned id = F->getIntrinsicID(true))
128 F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
129 return Upgraded;
119130 }
120131
121132 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
1717 #include "llvm/Support/LeakDetector.h"
1818 #include "llvm/Support/ManagedStatic.h"
1919 #include "SymbolTableListTraitsImpl.h"
20 #include "llvm/ADT/BitVector.h"
2021 #include "llvm/ADT/StringExtras.h"
2122 using namespace llvm;
2223
434435 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
435436 }
436437
438 const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
439 static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
440
441 if (IntrinsicAttributes[id])
442 return IntrinsicAttributes[id];
443
444 ParamAttrsVector Attrs;
445 uint16_t Attr = ParamAttr::None;
446
447 #define GET_INTRINSIC_ATTRIBUTES
448 #include "llvm/Intrinsics.gen"
449 #undef GET_INTRINSIC_ATTRIBUTES
450
451 // Intrinsics cannot throw exceptions.
452 Attr |= ParamAttr::NoUnwind;
453
454 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
455 IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
456 return IntrinsicAttributes[id];
457 }
458
437459 Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
438460 unsigned numTys) {
439 // There can never be multiple globals with the same name of different types,
440 // because intrinsics must be a specific type.
441 return cast(M->getOrInsertFunction(getName(id, Tys, numTys),
442 getType(id, Tys, numTys)));
461 // There can never be multiple globals with the same name of different types,
462 // because intrinsics must be a specific type.
463 Function *F =
464 cast(M->getOrInsertFunction(getName(id, Tys, numTys),
465 getType(id, Tys, numTys)));
466 F->setParamAttrs(getParamAttrs(id));
467 return F;
443468 }
444469
445470 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
1212
1313 #include "llvm/Type.h"
1414 #include "llvm/Instructions.h"
15 #include "llvm/IntrinsicInst.h"
1615 #include "llvm/Function.h"
16 #include "llvm/Support/CallSite.h"
1717 #include "llvm/Support/LeakDetector.h"
1818 using namespace llvm;
1919
196196 return true;
197197 }
198198
199 // IntrinsicOnlyReadsMemory - Return true if the specified intrinsic doesn't
200 // have any side-effects or if it only reads memory.
201 static bool IntrinsicOnlyReadsMemory(unsigned IntrinsicID) {
202 #define GET_SIDE_EFFECT_INFO
203 #include "llvm/Intrinsics.gen"
204 #undef GET_SIDE_EFFECT_INFO
205 return false;
206 }
207
208199 /// mayWriteToMemory - Return true if this instruction may modify memory.
209200 ///
210201 bool Instruction::mayWriteToMemory() const {
211202 switch (getOpcode()) {
212203 default: return false;
213204 case Instruction::Free:
205 case Instruction::Invoke:
214206 case Instruction::Store:
215 case Instruction::Invoke:
216207 case Instruction::VAArg:
217208 return true;
218209 case Instruction::Call:
219 if (const IntrinsicInst *II = dyn_cast(this)) {
220 // If the intrinsic doesn't write memory, it is safe.
221 return !IntrinsicOnlyReadsMemory(II->getIntrinsicID());
222 }
223 return true;
210 return !cast(this)->onlyReadsMemory();
224211 case Instruction::Load:
225212 return cast(this)->isVolatile();
226213 }
5252 else
5353 return cast(I)->paramHasAttr(i, attr);
5454 }
55
55 bool CallSite::doesNotAccessMemory() const {
56 if (CallInst *CI = dyn_cast(I))
57 return CI->doesNotAccessMemory();
58 else
59 return cast(I)->doesNotAccessMemory();
60 }
61 bool CallSite::onlyReadsMemory() const {
62 if (CallInst *CI = dyn_cast(I))
63 return CI->onlyReadsMemory();
64 else
65 return cast(I)->onlyReadsMemory();
66 }
5667
5768
5869
134134 // Name mangling occurs as follows:
135135 // - If V is an intrinsic function, do not change name at all
136136 // - Otherwise, mangling occurs if global collides with existing name.
137 if (isa(GV) && cast(GV)->getIntrinsicID()) {
137 if (isa(GV) && cast(GV)->isIntrinsic()) {
138138 Name = GV->getName(); // Is an intrinsic function
139139 } else if (!GV->hasName()) {
140140 // Must mangle the global into a unique ID.
684684 // Use the function we just added to get addresses of functions we need.
685685 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
686686 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
687 F->getIntrinsicID() == 0 /* ignore intrinsics */) {
687 !F->isIntrinsic() /* ignore intrinsics */) {
688688 Function *TestFn = Test->getFunction(F->getName());
689689
690690 // Don't forward functions which are external in the test module too.
4141 // Emit the intrinsic declaration generator.
4242 EmitGenerator(Ints, OS);
4343
44 // Emit mod/ref info for each function.
45 EmitModRefInfo(Ints, OS);
46
47 // Emit table of non-memory accessing intrinsics.
48 EmitNoMemoryInfo(Ints, OS);
49
50 // Emit side effect info for each intrinsic.
51 EmitSideEffectInfo(Ints, OS);
44 // Emit the intrinsic parameter attributes.
45 EmitAttributes(Ints, OS);
5246
5347 // Emit a list of intrinsics with corresponding GCC builtins.
5448 EmitGCCBuiltinList(Ints, OS);
296290 OS << "#endif\n\n";
297291 }
298292
299 void IntrinsicEmitter::EmitModRefInfo(const std::vector &Ints,
300 std::ostream &OS) {
301 OS << "// BasicAliasAnalysis code.\n";
302 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
303 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
304 switch (Ints[i].ModRef) {
305 default: break;
306 case CodeGenIntrinsic::NoMem:
307 OS << " NoMemoryIntrinsics->set(Intrinsic::" << Ints[i].EnumName << ");\n";
308 break;
309 case CodeGenIntrinsic::ReadArgMem:
310 case CodeGenIntrinsic::ReadMem:
311 OS << " OnlyReadsMemoryIntrinsics->set(Intrinsic::" << Ints[i].EnumName << ");\n";
312 break;
313 }
314 }
315 OS << "#endif\n\n";
316 }
317
318 void IntrinsicEmitter::
319 EmitNoMemoryInfo(const std::vector &Ints, std::ostream &OS) {
320 OS << "// SelectionDAGIsel code.\n";
321 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
322 OS << " switch (IntrinsicID) {\n";
293 void IntrinsicEmitter::
294 EmitAttributes(const std::vector &Ints, std::ostream &OS) {
295 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
296 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
297 OS << " switch (id) {\n";
323298 OS << " default: break;\n";
324299 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
325300 switch (Ints[i].ModRef) {
329304 break;
330305 }
331306 }
332 OS << " return true; // These intrinsics do not reference memory.\n";
333 OS << " }\n";
334 OS << "#endif\n\n";
335 }
336
337 void IntrinsicEmitter::
338 EmitSideEffectInfo(const std::vector &Ints, std::ostream &OS){
339 OS << "// Return true if doesn't access or only reads memory.\n";
340 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
341 OS << " switch (IntrinsicID) {\n";
342 OS << " default: break;\n";
307 OS << " Attr |= ParamAttr::ReadNone; // These do not access memory.\n";
308 OS << " break;\n";
343309 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
344310 switch (Ints[i].ModRef) {
345311 default: break;
346 case CodeGenIntrinsic::NoMem:
347312 case CodeGenIntrinsic::ReadArgMem:
348313 case CodeGenIntrinsic::ReadMem:
349314 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
350315 break;
351316 }
352317 }
353 OS << " return true; // These intrinsics have no side effects.\n";
318 OS << " Attr |= ParamAttr::ReadOnly; // These do not write memory.\n";
319 OS << " break;\n";
354320 OS << " }\n";
355321 OS << "#endif\n\n";
356322 }
3636 std::ostream &OS);
3737 void EmitGenerator(const std::vector &Ints,
3838 std::ostream &OS);
39 void EmitModRefInfo(const std::vector &Ints,
39 void EmitAttributes(const std::vector &Ints,
4040 std::ostream &OS);
41 void EmitNoMemoryInfo(const std::vector &Ints,
42 std::ostream &OS);
43 void EmitSideEffectInfo(const std::vector &Ints,
44 std::ostream &OS);
4541 void EmitGCCBuiltinList(const std::vector &Ints,
4642 std::ostream &OS);
4743 void EmitIntrinsicToGCCBuiltinMap(const std::vector &Ints,