blob: 01d180e4abe71cee6bd62397decc375bfba50ba0 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000157/// hasConstantOrUndefValue - Whether the specified PHI node always merges
158/// together the same value, assuming that undefs result in the same value as
159/// non-undefs.
160/// Unlike \ref hasConstantValue, this does not return a value because the
161/// unique non-undef incoming value need not dominate the PHI node.
162bool PHINode::hasConstantOrUndefValue() const {
163 Value *ConstantValue = nullptr;
164 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
165 Value *Incoming = getIncomingValue(i);
166 if (Incoming != this && !isa<UndefValue>(Incoming)) {
167 if (ConstantValue && ConstantValue != Incoming)
168 return false;
169 ConstantValue = Incoming;
170 }
171 }
172 return true;
173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
David Majnemer7fddecc2015-06-17 20:52:32 +0000179LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
180 const Twine &NameStr, Instruction *InsertBefore)
181 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
182 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000183}
184
David Majnemer7fddecc2015-06-17 20:52:32 +0000185LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
186 const Twine &NameStr, BasicBlock *InsertAtEnd)
187 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
188 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000189}
190
191LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000192 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
193 LP.getNumOperands()),
194 ReservedSpace(LP.getNumOperands()) {
195 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000196 Use *OL = getOperandList();
197 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
David Majnemer7fddecc2015-06-17 20:52:32 +0000204LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 const Twine &NameStr,
206 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000207 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
David Majnemer7fddecc2015-06-17 20:52:32 +0000210LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000213 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000218 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000219 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 setName(NameStr);
221 setCleanup(false);
222}
223
224/// growOperands - grow operands - This grows the operand list in response to a
225/// push_back style of operation. This grows the number of ops by 2 times.
226void LandingPadInst::growOperands(unsigned Size) {
227 unsigned e = getNumOperands();
228 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000229 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000230 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000231}
232
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000233void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 unsigned OpNo = getNumOperands();
235 growOperands(1);
236 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000237 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000238 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
David Blaikie348de692015-04-23 21:36:23 +0000248void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000249 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000251 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
252 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000267
268 auto It = populateBundleOperandInfos(Bundles, Args.size());
269 (void)It;
270 assert(It + 1 == op_end() && "Should add up!");
271
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Jay Foad5bd375a2011-07-15 08:37:34 +0000275void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000276 FTy =
277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000278 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000279 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282
283 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Daniel Dunbar4975db62009-07-25 04:41:11 +0000286CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 Instruction::Call,
291 OperandTraits<CallInst>::op_end(this) - 1,
292 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Daniel Dunbar4975db62009-07-25 04:41:11 +0000296CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 BasicBlock *InsertAtEnd)
298 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
299 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 Instruction::Call,
301 OperandTraits<CallInst>::op_end(this) - 1,
302 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000303 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000307 : Instruction(CI.getType(), Instruction::Call,
308 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
309 CI.getNumOperands()),
310 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000311 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000312 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000313
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000315 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
316 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000317 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Sanjoy Das2d161452015-11-18 06:23:38 +0000320CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
321 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000322 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000323
324 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
325 InsertPt);
326 NewCI->setTailCallKind(CI->getTailCallKind());
327 NewCI->setCallingConv(CI->getCallingConv());
328 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000329 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000330 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000331 return NewCI;
332}
333
Amaury Sechet392638d2016-06-14 20:27:35 +0000334void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000335 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000336 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000337 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000338}
339
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000340void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
341 AttributeSet PAL = getAttributes();
342 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
343 setAttributes(PAL);
344}
345
Amaury Sechet392638d2016-06-14 20:27:35 +0000346void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000347 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000348 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000349 setAttributes(PAL);
350}
351
Amaury Sechet392638d2016-06-14 20:27:35 +0000352void CallInst::removeAttribute(unsigned i, Attribute Attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000353 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000354 AttrBuilder B(Attr);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000355 LLVMContext &Context = getContext();
356 PAL = PAL.removeAttributes(Context, i,
357 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000358 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000359}
360
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000361void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
362 AttributeSet PAL = getAttributes();
363 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
364 setAttributes(PAL);
365}
366
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000367void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
368 AttributeSet PAL = getAttributes();
369 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
370 setAttributes(PAL);
371}
372
Amaury Sechet392638d2016-06-14 20:27:35 +0000373bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000374 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
375
Amaury Sechet392638d2016-06-14 20:27:35 +0000376 if (AttributeList.hasAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000377 return true;
378 if (const Function *F = getCalledFunction())
Amaury Sechet392638d2016-06-14 20:27:35 +0000379 return F->getAttributes().hasAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000380 return false;
381}
382
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000383bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000384 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000385 // There are getNumOperands() - 1 data operands. The last operand is the
386 // callee.
387 assert(i < getNumOperands() && "Data operand index out of bounds!");
388
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000389 // The attribute A can either be directly specified, if the operand in
390 // question is a call argument; or be indirectly implied by the kind of its
391 // containing operand bundle, if the operand is a bundle operand.
392
393 if (i < (getNumArgOperands() + 1))
Amaury Sechet392638d2016-06-14 20:27:35 +0000394 return paramHasAttr(i, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000395
396 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
397 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000398 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000399}
400
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000401/// IsConstantOne - Return true only if val is constant int 1
402static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000403 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000404 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
405 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406}
407
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000408static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000409 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000410 Type *AllocTy, Value *AllocSize,
411 Value *ArraySize,
412 ArrayRef<OperandBundleDef> OpB,
413 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000414 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000415 "createMalloc needs either InsertBefore or InsertAtEnd");
416
417 // malloc(type) becomes:
418 // bitcast (i8* malloc(typeSize)) to type*
419 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000420 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000421 if (!ArraySize)
422 ArraySize = ConstantInt::get(IntPtrTy, 1);
423 else if (ArraySize->getType() != IntPtrTy) {
424 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000425 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
426 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000428 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
429 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000430 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000432 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000433 if (IsConstantOne(AllocSize)) {
434 AllocSize = ArraySize; // Operand * 1 = Operand
435 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
436 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
437 false /*ZExt*/);
438 // Malloc arg is constant product of type size and array size
439 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
440 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000441 // Multiply type size by the array size...
442 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000443 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
444 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
447 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000448 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000449 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000450
Victor Hernandez788eaab2009-09-18 19:20:02 +0000451 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000452 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000453 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
454 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000455 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000456 Value *MallocFunc = MallocF;
457 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000458 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000459 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000460 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000461 CallInst *MCall = nullptr;
462 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000463 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000464 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
465 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000466 Result = MCall;
467 if (Result->getType() != AllocPtrType)
468 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000470 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000471 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000472 Result = MCall;
473 if (Result->getType() != AllocPtrType) {
474 InsertAtEnd->getInstList().push_back(MCall);
475 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000476 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000477 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000478 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000479 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000480 if (Function *F = dyn_cast<Function>(MallocFunc)) {
481 MCall->setCallingConv(F->getCallingConv());
482 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
483 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000484 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000485
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000486 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000487}
488
489/// CreateMalloc - Generate the IR for a call to malloc:
490/// 1. Compute the malloc call's argument as the specified type's size,
491/// possibly multiplied by the array size if the array size is not
492/// constant 1.
493/// 2. Call malloc with that argument.
494/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000495Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000496 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000497 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000498 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000499 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000500 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000501 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502}
David Majnemerfadc6db2016-04-29 08:07:22 +0000503Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
504 Type *IntPtrTy, Type *AllocTy,
505 Value *AllocSize, Value *ArraySize,
506 ArrayRef<OperandBundleDef> OpB,
507 Function *MallocF,
508 const Twine &Name) {
509 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
510 ArraySize, OpB, MallocF, Name);
511}
512
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000513
514/// CreateMalloc - Generate the IR for a call to malloc:
515/// 1. Compute the malloc call's argument as the specified type's size,
516/// possibly multiplied by the array size if the array size is not
517/// constant 1.
518/// 2. Call malloc with that argument.
519/// 3. Bitcast the result of the malloc call to the specified type.
520/// Note: This function does not add the bitcast to the basic block, that is the
521/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000522Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000523 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000524 Value *AllocSize, Value *ArraySize,
525 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000526 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000527 ArraySize, None, MallocF, Name);
528}
529Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
530 Type *IntPtrTy, Type *AllocTy,
531 Value *AllocSize, Value *ArraySize,
532 ArrayRef<OperandBundleDef> OpB,
533 Function *MallocF, const Twine &Name) {
534 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
535 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000536}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000537
David Majnemerfadc6db2016-04-29 08:07:22 +0000538static Instruction *createFree(Value *Source,
539 ArrayRef<OperandBundleDef> Bundles,
540 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000541 BasicBlock *InsertAtEnd) {
542 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
543 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000544 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000545 "Can not free something of nonpointer type!");
546
Ana Pazosb3596022016-02-03 21:34:39 +0000547 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
548 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000549
Chris Lattner229907c2011-07-18 04:54:35 +0000550 Type *VoidTy = Type::getVoidTy(M->getContext());
551 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000552 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000553 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Ana Pazosb3596022016-02-03 21:34:39 +0000554 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000555 Value *PtrCast = Source;
556 if (InsertBefore) {
557 if (Source->getType() != IntPtrTy)
558 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000559 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000560 } else {
561 if (Source->getType() != IntPtrTy)
562 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000563 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000564 }
565 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000566 if (Function *F = dyn_cast<Function>(FreeFunc))
567 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000568
569 return Result;
570}
571
572/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000573Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000574 return createFree(Source, None, InsertBefore, nullptr);
575}
576Instruction *CallInst::CreateFree(Value *Source,
577 ArrayRef<OperandBundleDef> Bundles,
578 Instruction *InsertBefore) {
579 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000580}
581
582/// CreateFree - Generate the IR for a call to the builtin free function.
583/// Note: This function does not add the call to the basic block, that is the
584/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000585Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000586 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
587 assert(FreeCall && "CreateFree did not create a CallInst");
588 return FreeCall;
589}
590Instruction *CallInst::CreateFree(Value *Source,
591 ArrayRef<OperandBundleDef> Bundles,
592 BasicBlock *InsertAtEnd) {
593 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000594 assert(FreeCall && "CreateFree did not create a CallInst");
595 return FreeCall;
596}
597
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000598//===----------------------------------------------------------------------===//
599// InvokeInst Implementation
600//===----------------------------------------------------------------------===//
601
David Blaikie3e807092015-05-13 18:35:26 +0000602void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
603 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000604 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000605 const Twine &NameStr) {
606 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000607
Sanjoy Das9303c242015-09-24 19:14:18 +0000608 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
609 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000610 Op<-3>() = Fn;
611 Op<-2>() = IfNormal;
612 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000613
614#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000615 assert(((Args.size() == FTy->getNumParams()) ||
616 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000617 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000618
Jay Foad5bd375a2011-07-15 08:37:34 +0000619 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000620 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000621 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000622 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000623#endif
624
625 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000626
627 auto It = populateBundleOperandInfos(Bundles, Args.size());
628 (void)It;
629 assert(It + 3 == op_end() && "Should add up!");
630
Jay Foad5bd375a2011-07-15 08:37:34 +0000631 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000632}
633
Misha Brukmanb1c93172005-04-21 23:48:37 +0000634InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000635 : TerminatorInst(II.getType(), Instruction::Invoke,
636 OperandTraits<InvokeInst>::op_end(this) -
637 II.getNumOperands(),
638 II.getNumOperands()),
639 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000640 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000641 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000642 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
643 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000644 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645}
646
Sanjoy Das2d161452015-11-18 06:23:38 +0000647InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
648 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000649 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000650
651 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
652 II->getUnwindDest(), Args, OpB,
653 II->getName(), InsertPt);
654 NewII->setCallingConv(II->getCallingConv());
655 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000656 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000657 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000658 return NewII;
659}
660
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
662 return getSuccessor(idx);
663}
664unsigned InvokeInst::getNumSuccessorsV() const {
665 return getNumSuccessors();
666}
667void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
668 return setSuccessor(idx, B);
669}
670
Amaury Sechet392638d2016-06-14 20:27:35 +0000671bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000672 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
673
Amaury Sechet392638d2016-06-14 20:27:35 +0000674 if (AttributeList.hasAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000675 return true;
676 if (const Function *F = getCalledFunction())
Amaury Sechet392638d2016-06-14 20:27:35 +0000677 return F->getAttributes().hasAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000678 return false;
679}
680
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000681bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000682 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000683 // There are getNumOperands() - 3 data operands. The last three operands are
684 // the callee and the two successor basic blocks.
685 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
686
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000687 // The attribute A can either be directly specified, if the operand in
688 // question is an invoke argument; or be indirectly implied by the kind of its
689 // containing operand bundle, if the operand is a bundle operand.
690
691 if (i < (getNumArgOperands() + 1))
Amaury Sechet392638d2016-06-14 20:27:35 +0000692 return paramHasAttr(i, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000693
694 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
695 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000696 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000697}
698
Amaury Sechet392638d2016-06-14 20:27:35 +0000699void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000700 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000701 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000702 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000703}
704
Amaury Sechet392638d2016-06-14 20:27:35 +0000705void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000706 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000707 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000708 setAttributes(PAL);
709}
710
Amaury Sechet392638d2016-06-14 20:27:35 +0000711void InvokeInst::removeAttribute(unsigned i, Attribute Attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000712 AttributeSet PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000713 AttrBuilder B(Attr);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000714 PAL = PAL.removeAttributes(getContext(), i,
715 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000716 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000717}
718
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000719void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
720 AttributeSet PAL = getAttributes();
721 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
722 setAttributes(PAL);
723}
724
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000725void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
726 AttributeSet PAL = getAttributes();
727 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
728 setAttributes(PAL);
729}
730
Bill Wendlingfae14752011-08-12 20:24:12 +0000731LandingPadInst *InvokeInst::getLandingPadInst() const {
732 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
733}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000734
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735//===----------------------------------------------------------------------===//
736// ReturnInst Implementation
737//===----------------------------------------------------------------------===//
738
Chris Lattner2195fc42007-02-24 00:55:48 +0000739ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000740 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000741 OperandTraits<ReturnInst>::op_end(this) -
742 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000743 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000744 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000745 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000746 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000747}
748
Owen Anderson55f1c092009-08-13 21:58:54 +0000749ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
750 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000751 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
752 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000753 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000754 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755}
Owen Anderson55f1c092009-08-13 21:58:54 +0000756ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
757 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000758 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
759 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000760 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000761 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000762}
Owen Anderson55f1c092009-08-13 21:58:54 +0000763ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
764 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000765 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000766}
767
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768unsigned ReturnInst::getNumSuccessorsV() const {
769 return getNumSuccessors();
770}
771
Devang Patelae682fb2008-02-26 17:56:20 +0000772/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
773/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000775 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000779 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780}
781
Devang Patel59643e52008-02-23 00:35:18 +0000782ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000783}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000786// ResumeInst Implementation
787//===----------------------------------------------------------------------===//
788
789ResumeInst::ResumeInst(const ResumeInst &RI)
790 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
791 OperandTraits<ResumeInst>::op_begin(this), 1) {
792 Op<0>() = RI.Op<0>();
793}
794
795ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
796 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
797 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
798 Op<0>() = Exn;
799}
800
801ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
802 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
803 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
804 Op<0>() = Exn;
805}
806
807unsigned ResumeInst::getNumSuccessorsV() const {
808 return getNumSuccessors();
809}
810
811void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
812 llvm_unreachable("ResumeInst has no successors!");
813}
814
815BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
816 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000817}
818
819//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000820// CleanupReturnInst Implementation
821//===----------------------------------------------------------------------===//
822
823CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
824 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
825 OperandTraits<CleanupReturnInst>::op_end(this) -
826 CRI.getNumOperands(),
827 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000828 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000829 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000830 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000831 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000832}
833
David Majnemer8a1c45d2015-12-12 05:38:55 +0000834void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000835 if (UnwindBB)
836 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000837
David Majnemer8a1c45d2015-12-12 05:38:55 +0000838 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000839 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000840 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000841}
842
David Majnemer8a1c45d2015-12-12 05:38:55 +0000843CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
844 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000845 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
846 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000847 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
848 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000849 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000850}
851
David Majnemer8a1c45d2015-12-12 05:38:55 +0000852CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
853 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000854 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
855 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000856 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
857 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000858 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000859}
860
861BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
862 assert(Idx == 0);
863 return getUnwindDest();
864}
865unsigned CleanupReturnInst::getNumSuccessorsV() const {
866 return getNumSuccessors();
867}
868void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
869 assert(Idx == 0);
870 setUnwindDest(B);
871}
872
873//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000874// CatchReturnInst Implementation
875//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000876void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000877 Op<0>() = CatchPad;
878 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000879}
David Majnemer654e1302015-07-31 17:58:14 +0000880
881CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
882 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000883 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
884 Op<0>() = CRI.Op<0>();
885 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000886}
887
David Majnemer8a1c45d2015-12-12 05:38:55 +0000888CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000889 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000890 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000891 OperandTraits<CatchReturnInst>::op_begin(this), 2,
892 InsertBefore) {
893 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000894}
895
David Majnemer8a1c45d2015-12-12 05:38:55 +0000896CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000897 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000898 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000899 OperandTraits<CatchReturnInst>::op_begin(this), 2,
900 InsertAtEnd) {
901 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000902}
903
904BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000905 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000906 return getSuccessor();
907}
908unsigned CatchReturnInst::getNumSuccessorsV() const {
909 return getNumSuccessors();
910}
911void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000912 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000913 setSuccessor(B);
914}
915
916//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000917// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000918//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000919
920CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
921 unsigned NumReservedValues,
922 const Twine &NameStr,
923 Instruction *InsertBefore)
924 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
925 InsertBefore) {
926 if (UnwindDest)
927 ++NumReservedValues;
928 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000929 setName(NameStr);
930}
931
David Majnemer8a1c45d2015-12-12 05:38:55 +0000932CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
933 unsigned NumReservedValues,
934 const Twine &NameStr, BasicBlock *InsertAtEnd)
935 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000936 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000937 if (UnwindDest)
938 ++NumReservedValues;
939 init(ParentPad, UnwindDest, NumReservedValues + 1);
940 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000941}
942
David Majnemer8a1c45d2015-12-12 05:38:55 +0000943CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
944 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
945 CSI.getNumOperands()) {
946 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
947 setNumHungOffUseOperands(ReservedSpace);
948 Use *OL = getOperandList();
949 const Use *InOL = CSI.getOperandList();
950 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
951 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000952}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000953
954void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
955 unsigned NumReservedValues) {
956 assert(ParentPad && NumReservedValues);
957
958 ReservedSpace = NumReservedValues;
959 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
960 allocHungoffUses(ReservedSpace);
961
962 Op<0>() = ParentPad;
963 if (UnwindDest) {
964 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
965 setUnwindDest(UnwindDest);
966 }
967}
968
969/// growOperands - grow operands - This grows the operand list in response to a
970/// push_back style of operation. This grows the number of ops by 2 times.
971void CatchSwitchInst::growOperands(unsigned Size) {
972 unsigned NumOperands = getNumOperands();
973 assert(NumOperands >= 1);
974 if (ReservedSpace >= NumOperands + Size)
975 return;
976 ReservedSpace = (NumOperands + Size / 2) * 2;
977 growHungoffUses(ReservedSpace);
978}
979
980void CatchSwitchInst::addHandler(BasicBlock *Handler) {
981 unsigned OpNo = getNumOperands();
982 growOperands(1);
983 assert(OpNo < ReservedSpace && "Growing didn't work!");
984 setNumHungOffUseOperands(getNumOperands() + 1);
985 getOperandList()[OpNo] = Handler;
986}
987
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000988void CatchSwitchInst::removeHandler(handler_iterator HI) {
989 // Move all subsequent handlers up one.
990 Use *EndDst = op_end() - 1;
991 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
992 *CurDst = *(CurDst + 1);
993 // Null out the last handler use.
994 *EndDst = nullptr;
995
996 setNumHungOffUseOperands(getNumOperands() - 1);
997}
998
David Majnemer8a1c45d2015-12-12 05:38:55 +0000999BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1000 return getSuccessor(idx);
1001}
1002unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001003 return getNumSuccessors();
1004}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001005void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1006 setSuccessor(idx, B);
1007}
1008
1009//===----------------------------------------------------------------------===//
1010// FuncletPadInst Implementation
1011//===----------------------------------------------------------------------===//
1012void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1013 const Twine &NameStr) {
1014 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1015 std::copy(Args.begin(), Args.end(), op_begin());
1016 setParentPad(ParentPad);
1017 setName(NameStr);
1018}
1019
1020FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1021 : Instruction(FPI.getType(), FPI.getOpcode(),
1022 OperandTraits<FuncletPadInst>::op_end(this) -
1023 FPI.getNumOperands(),
1024 FPI.getNumOperands()) {
1025 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1026 setParentPad(FPI.getParentPad());
1027}
1028
1029FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1030 ArrayRef<Value *> Args, unsigned Values,
1031 const Twine &NameStr, Instruction *InsertBefore)
1032 : Instruction(ParentPad->getType(), Op,
1033 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1034 InsertBefore) {
1035 init(ParentPad, Args, NameStr);
1036}
1037
1038FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1039 ArrayRef<Value *> Args, unsigned Values,
1040 const Twine &NameStr, BasicBlock *InsertAtEnd)
1041 : Instruction(ParentPad->getType(), Op,
1042 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1043 InsertAtEnd) {
1044 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001045}
1046
1047//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001048// UnreachableInst Implementation
1049//===----------------------------------------------------------------------===//
1050
Owen Anderson55f1c092009-08-13 21:58:54 +00001051UnreachableInst::UnreachableInst(LLVMContext &Context,
1052 Instruction *InsertBefore)
1053 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001054 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001055}
Owen Anderson55f1c092009-08-13 21:58:54 +00001056UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1057 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001058 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001059}
1060
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061unsigned UnreachableInst::getNumSuccessorsV() const {
1062 return getNumSuccessors();
1063}
1064
1065void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001066 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067}
1068
1069BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001070 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001071}
1072
1073//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001074// BranchInst Implementation
1075//===----------------------------------------------------------------------===//
1076
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001077void BranchInst::AssertOK() {
1078 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001079 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001080 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081}
1082
Chris Lattner2195fc42007-02-24 00:55:48 +00001083BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001084 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001085 OperandTraits<BranchInst>::op_end(this) - 1,
1086 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001087 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001088 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001089}
1090BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1091 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001092 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001093 OperandTraits<BranchInst>::op_end(this) - 3,
1094 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001095 Op<-1>() = IfTrue;
1096 Op<-2>() = IfFalse;
1097 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001098#ifndef NDEBUG
1099 AssertOK();
1100#endif
1101}
1102
1103BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001104 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001105 OperandTraits<BranchInst>::op_end(this) - 1,
1106 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001107 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001108 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001109}
1110
1111BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1112 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001114 OperandTraits<BranchInst>::op_end(this) - 3,
1115 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001116 Op<-1>() = IfTrue;
1117 Op<-2>() = IfFalse;
1118 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001119#ifndef NDEBUG
1120 AssertOK();
1121#endif
1122}
1123
1124
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001125BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001126 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001127 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1128 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001129 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001130 if (BI.getNumOperands() != 1) {
1131 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001132 Op<-3>() = BI.Op<-3>();
1133 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001134 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001135 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136}
1137
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001138void BranchInst::swapSuccessors() {
1139 assert(isConditional() &&
1140 "Cannot swap successors of an unconditional branch");
1141 Op<-1>().swap(Op<-2>());
1142
1143 // Update profile metadata if present and it matches our structural
1144 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001145 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001146 if (!ProfileData || ProfileData->getNumOperands() != 3)
1147 return;
1148
1149 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001150 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1151 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001152 setMetadata(LLVMContext::MD_prof,
1153 MDNode::get(ProfileData->getContext(), Ops));
1154}
1155
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001156BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1157 return getSuccessor(idx);
1158}
1159unsigned BranchInst::getNumSuccessorsV() const {
1160 return getNumSuccessors();
1161}
1162void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1163 setSuccessor(idx, B);
1164}
1165
1166
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001167//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001168// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001169//===----------------------------------------------------------------------===//
1170
Owen Andersonb6b25302009-07-14 23:09:55 +00001171static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001172 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001173 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001174 else {
1175 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001176 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001177 assert(Amt->getType()->isIntegerTy() &&
1178 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001179 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001180 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001181}
1182
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001183AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1184 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001185
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001186AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1187 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001188
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001189AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001190 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001191 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001192
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001193AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001194 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001195 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001196
Chris Lattner229907c2011-07-18 04:54:35 +00001197AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001198 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001199 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1200 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1201 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001202 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001203 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001204 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001205}
1206
Chris Lattner229907c2011-07-18 04:54:35 +00001207AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001208 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001209 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1210 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1211 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001212 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001213 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001214 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215}
1216
Gordon Henriksen14a55692007-12-10 02:14:30 +00001217// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001218AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001219}
1220
Victor Hernandez8acf2952009-10-23 21:09:37 +00001221void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001222 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001223 assert(Align <= MaximumAlignment &&
1224 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001225 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1226 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001227 assert(getAlignment() == Align && "Alignment representation error!");
1228}
1229
Victor Hernandez8acf2952009-10-23 21:09:37 +00001230bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001231 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001232 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001233 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234}
1235
Chris Lattner8b291e62008-11-26 02:54:17 +00001236/// isStaticAlloca - Return true if this alloca is in the entry block of the
1237/// function and is a constant size. If so, the code generator will fold it
1238/// into the prolog/epilog code, so it is basically free.
1239bool AllocaInst::isStaticAlloca() const {
1240 // Must be constant size.
1241 if (!isa<ConstantInt>(getArraySize())) return false;
1242
1243 // Must be in the entry block.
1244 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001245 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001246}
1247
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001248//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001249// LoadInst Implementation
1250//===----------------------------------------------------------------------===//
1251
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001252void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001253 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001254 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001255 assert(!(isAtomic() && getAlignment() == 0) &&
1256 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001257}
1258
Daniel Dunbar4975db62009-07-25 04:41:11 +00001259LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001260 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001261
Daniel Dunbar4975db62009-07-25 04:41:11 +00001262LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001263 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001264
David Blaikie0c28fd72015-05-20 21:46:30 +00001265LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001266 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001267 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001268
1269LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1270 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001271 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001272
David Blaikieb7a029872015-04-17 19:56:21 +00001273LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001274 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001275 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1276 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001278LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001279 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001280 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1281 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001282
David Blaikie15d9a4c2015-04-06 20:59:48 +00001283LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001284 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001285 SynchronizationScope SynchScope, Instruction *InsertBef)
1286 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001287 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001288 setVolatile(isVolatile);
1289 setAlignment(Align);
1290 setAtomic(Order, SynchScope);
1291 AssertOK();
1292 setName(Name);
1293}
1294
1295LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1296 unsigned Align, AtomicOrdering Order,
1297 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001299 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001300 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001301 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001302 setAlignment(Align);
1303 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001304 AssertOK();
1305 setName(Name);
1306}
1307
Daniel Dunbar27096822009-08-11 18:11:15 +00001308LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1309 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1310 Load, Ptr, InsertBef) {
1311 setVolatile(false);
1312 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001313 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001314 AssertOK();
1315 if (Name && Name[0]) setName(Name);
1316}
1317
1318LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1319 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1320 Load, Ptr, InsertAE) {
1321 setVolatile(false);
1322 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001323 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001324 AssertOK();
1325 if (Name && Name[0]) setName(Name);
1326}
1327
David Blaikie0c28fd72015-05-20 21:46:30 +00001328LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001329 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001330 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1331 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001332 setVolatile(isVolatile);
1333 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001334 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001335 AssertOK();
1336 if (Name && Name[0]) setName(Name);
1337}
1338
1339LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1340 BasicBlock *InsertAE)
1341 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1342 Load, Ptr, InsertAE) {
1343 setVolatile(isVolatile);
1344 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001345 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001346 AssertOK();
1347 if (Name && Name[0]) setName(Name);
1348}
1349
Christopher Lamb84485702007-04-22 19:24:39 +00001350void LoadInst::setAlignment(unsigned Align) {
1351 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001352 assert(Align <= MaximumAlignment &&
1353 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001354 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001355 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001356 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001357}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001358
1359//===----------------------------------------------------------------------===//
1360// StoreInst Implementation
1361//===----------------------------------------------------------------------===//
1362
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001363void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001364 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001365 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001366 "Ptr must have pointer type!");
1367 assert(getOperand(0)->getType() ==
1368 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001369 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001370 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001371 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001372}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001373
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001374StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001375 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001376
1377StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001378 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001379
Misha Brukmanb1c93172005-04-21 23:48:37 +00001380StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001381 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001382 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001383
1384StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001385 BasicBlock *InsertAtEnd)
1386 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1387
1388StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1389 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001390 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1391 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001392
1393StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1394 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001395 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1396 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001397
Misha Brukmanb1c93172005-04-21 23:48:37 +00001398StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001399 unsigned Align, AtomicOrdering Order,
1400 SynchronizationScope SynchScope,
1401 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001402 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001403 OperandTraits<StoreInst>::op_begin(this),
1404 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001405 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001406 Op<0>() = val;
1407 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001408 setVolatile(isVolatile);
1409 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001410 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001411 AssertOK();
1412}
1413
1414StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001415 unsigned Align, AtomicOrdering Order,
1416 SynchronizationScope SynchScope,
1417 BasicBlock *InsertAtEnd)
1418 : Instruction(Type::getVoidTy(val->getContext()), Store,
1419 OperandTraits<StoreInst>::op_begin(this),
1420 OperandTraits<StoreInst>::operands(this),
1421 InsertAtEnd) {
1422 Op<0>() = val;
1423 Op<1>() = addr;
1424 setVolatile(isVolatile);
1425 setAlignment(Align);
1426 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001427 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428}
1429
Christopher Lamb84485702007-04-22 19:24:39 +00001430void StoreInst::setAlignment(unsigned Align) {
1431 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001432 assert(Align <= MaximumAlignment &&
1433 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001434 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001435 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001436 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001437}
1438
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001439//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001440// AtomicCmpXchgInst Implementation
1441//===----------------------------------------------------------------------===//
1442
1443void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001444 AtomicOrdering SuccessOrdering,
1445 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001446 SynchronizationScope SynchScope) {
1447 Op<0>() = Ptr;
1448 Op<1>() = Cmp;
1449 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001450 setSuccessOrdering(SuccessOrdering);
1451 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001452 setSynchScope(SynchScope);
1453
1454 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1455 "All operands must be non-null!");
1456 assert(getOperand(0)->getType()->isPointerTy() &&
1457 "Ptr must have pointer type!");
1458 assert(getOperand(1)->getType() ==
1459 cast<PointerType>(getOperand(0)->getType())->getElementType()
1460 && "Ptr must be a pointer to Cmp type!");
1461 assert(getOperand(2)->getType() ==
1462 cast<PointerType>(getOperand(0)->getType())->getElementType()
1463 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001464 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001465 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001466 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001467 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001468 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1469 "AtomicCmpXchg failure argument shall be no stronger than the success "
1470 "argument");
1471 assert(FailureOrdering != AtomicOrdering::Release &&
1472 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001473 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001474}
1475
1476AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001477 AtomicOrdering SuccessOrdering,
1478 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001479 SynchronizationScope SynchScope,
1480 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001481 : Instruction(
1482 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1483 nullptr),
1484 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1485 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001486 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001487}
1488
1489AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001490 AtomicOrdering SuccessOrdering,
1491 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001492 SynchronizationScope SynchScope,
1493 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001494 : Instruction(
1495 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1496 nullptr),
1497 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1498 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001499 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001500}
Tim Northover420a2162014-06-13 14:24:07 +00001501
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001502//===----------------------------------------------------------------------===//
1503// AtomicRMWInst Implementation
1504//===----------------------------------------------------------------------===//
1505
1506void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1507 AtomicOrdering Ordering,
1508 SynchronizationScope SynchScope) {
1509 Op<0>() = Ptr;
1510 Op<1>() = Val;
1511 setOperation(Operation);
1512 setOrdering(Ordering);
1513 setSynchScope(SynchScope);
1514
1515 assert(getOperand(0) && getOperand(1) &&
1516 "All operands must be non-null!");
1517 assert(getOperand(0)->getType()->isPointerTy() &&
1518 "Ptr must have pointer type!");
1519 assert(getOperand(1)->getType() ==
1520 cast<PointerType>(getOperand(0)->getType())->getElementType()
1521 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001522 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001523 "AtomicRMW instructions must be atomic!");
1524}
1525
1526AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1527 AtomicOrdering Ordering,
1528 SynchronizationScope SynchScope,
1529 Instruction *InsertBefore)
1530 : Instruction(Val->getType(), AtomicRMW,
1531 OperandTraits<AtomicRMWInst>::op_begin(this),
1532 OperandTraits<AtomicRMWInst>::operands(this),
1533 InsertBefore) {
1534 Init(Operation, Ptr, Val, Ordering, SynchScope);
1535}
1536
1537AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1538 AtomicOrdering Ordering,
1539 SynchronizationScope SynchScope,
1540 BasicBlock *InsertAtEnd)
1541 : Instruction(Val->getType(), AtomicRMW,
1542 OperandTraits<AtomicRMWInst>::op_begin(this),
1543 OperandTraits<AtomicRMWInst>::operands(this),
1544 InsertAtEnd) {
1545 Init(Operation, Ptr, Val, Ordering, SynchScope);
1546}
1547
1548//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001549// FenceInst Implementation
1550//===----------------------------------------------------------------------===//
1551
1552FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1553 SynchronizationScope SynchScope,
1554 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001555 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001556 setOrdering(Ordering);
1557 setSynchScope(SynchScope);
1558}
1559
1560FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1561 SynchronizationScope SynchScope,
1562 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001563 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001564 setOrdering(Ordering);
1565 setSynchScope(SynchScope);
1566}
1567
1568//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569// GetElementPtrInst Implementation
1570//===----------------------------------------------------------------------===//
1571
Pete Cooper1d078692015-12-14 20:29:16 +00001572void GetElementPtrInst::anchor() {}
1573
Jay Foadd1b78492011-07-25 09:48:08 +00001574void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001575 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001576 assert(getNumOperands() == 1 + IdxList.size() &&
1577 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001578 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001579 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001580 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001581}
1582
Gabor Greiff6caff662008-05-10 08:32:32 +00001583GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001584 : Instruction(GEPI.getType(), GetElementPtr,
1585 OperandTraits<GetElementPtrInst>::op_end(this) -
1586 GEPI.getNumOperands(),
1587 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001588 SourceElementType(GEPI.SourceElementType),
1589 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001590 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001591 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001592}
1593
Chris Lattner6090a422009-03-09 04:46:40 +00001594/// getIndexedType - Returns the type of the element that would be accessed with
1595/// a gep instruction with the specified parameters.
1596///
1597/// The Idxs pointer should point to a continuous piece of memory containing the
1598/// indices, either as Value* or uint64_t.
1599///
1600/// A null type is returned if the indices are invalid for the specified
1601/// pointer type.
1602///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001603template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001604static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001605 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001606 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001607 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001608
Chris Lattner6090a422009-03-09 04:46:40 +00001609 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001610 // it cannot be 'stepped over'.
1611 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001612 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001613
Dan Gohman1ecaf452008-05-31 00:58:22 +00001614 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001615 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001616 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001617 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001618 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001619 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001620 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001621 }
Craig Topperc6207612014-04-09 06:08:46 +00001622 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623}
1624
David Blaikied288fb82015-03-30 21:41:43 +00001625Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1626 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001627}
1628
David Blaikied288fb82015-03-30 21:41:43 +00001629Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001630 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001631 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001632}
1633
David Blaikied288fb82015-03-30 21:41:43 +00001634Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1635 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001636}
1637
Chris Lattner45f15572007-04-14 00:12:57 +00001638/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1639/// zeros. If so, the result pointer and the first operand have the same
1640/// value, just potentially different types.
1641bool GetElementPtrInst::hasAllZeroIndices() const {
1642 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1643 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1644 if (!CI->isZero()) return false;
1645 } else {
1646 return false;
1647 }
1648 }
1649 return true;
1650}
1651
Chris Lattner27058292007-04-27 20:35:56 +00001652/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1653/// constant integers. If so, the result pointer and the first operand have
1654/// a constant offset between them.
1655bool GetElementPtrInst::hasAllConstantIndices() const {
1656 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1657 if (!isa<ConstantInt>(getOperand(i)))
1658 return false;
1659 }
1660 return true;
1661}
1662
Dan Gohman1b849082009-09-07 23:54:19 +00001663void GetElementPtrInst::setIsInBounds(bool B) {
1664 cast<GEPOperator>(this)->setIsInBounds(B);
1665}
Chris Lattner45f15572007-04-14 00:12:57 +00001666
Nick Lewycky28a5f252009-09-27 21:33:04 +00001667bool GetElementPtrInst::isInBounds() const {
1668 return cast<GEPOperator>(this)->isInBounds();
1669}
1670
Chandler Carruth1e140532012-12-11 10:29:10 +00001671bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1672 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001673 // Delegate to the generic GEPOperator implementation.
1674 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001675}
1676
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001678// ExtractElementInst Implementation
1679//===----------------------------------------------------------------------===//
1680
1681ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001682 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001683 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001684 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001685 ExtractElement,
1686 OperandTraits<ExtractElementInst>::op_begin(this),
1687 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001688 assert(isValidOperands(Val, Index) &&
1689 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001690 Op<0>() = Val;
1691 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001692 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001693}
1694
1695ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001696 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001697 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001698 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001699 ExtractElement,
1700 OperandTraits<ExtractElementInst>::op_begin(this),
1701 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001702 assert(isValidOperands(Val, Index) &&
1703 "Invalid extractelement instruction operands!");
1704
Gabor Greif2d3024d2008-05-26 21:33:52 +00001705 Op<0>() = Val;
1706 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001707 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001708}
1709
Chris Lattner65511ff2006-10-05 06:24:58 +00001710
Chris Lattner54865b32006-04-08 04:05:48 +00001711bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001712 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001713 return false;
1714 return true;
1715}
1716
1717
Robert Bocchino23004482006-01-10 19:05:34 +00001718//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001719// InsertElementInst Implementation
1720//===----------------------------------------------------------------------===//
1721
Chris Lattner54865b32006-04-08 04:05:48 +00001722InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001723 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001724 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001725 : Instruction(Vec->getType(), InsertElement,
1726 OperandTraits<InsertElementInst>::op_begin(this),
1727 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001728 assert(isValidOperands(Vec, Elt, Index) &&
1729 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001730 Op<0>() = Vec;
1731 Op<1>() = Elt;
1732 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001733 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001734}
1735
Chris Lattner54865b32006-04-08 04:05:48 +00001736InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001737 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001738 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001739 : Instruction(Vec->getType(), InsertElement,
1740 OperandTraits<InsertElementInst>::op_begin(this),
1741 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001742 assert(isValidOperands(Vec, Elt, Index) &&
1743 "Invalid insertelement instruction operands!");
1744
Gabor Greif2d3024d2008-05-26 21:33:52 +00001745 Op<0>() = Vec;
1746 Op<1>() = Elt;
1747 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001748 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001749}
1750
Chris Lattner54865b32006-04-08 04:05:48 +00001751bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1752 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001753 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001754 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001755
Reid Spencerd84d35b2007-02-15 02:26:10 +00001756 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001757 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001758
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001759 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001760 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001761 return true;
1762}
1763
1764
Robert Bocchinoca27f032006-01-17 20:07:22 +00001765//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001766// ShuffleVectorInst Implementation
1767//===----------------------------------------------------------------------===//
1768
1769ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001770 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001771 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001772: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001773 cast<VectorType>(Mask->getType())->getNumElements()),
1774 ShuffleVector,
1775 OperandTraits<ShuffleVectorInst>::op_begin(this),
1776 OperandTraits<ShuffleVectorInst>::operands(this),
1777 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001778 assert(isValidOperands(V1, V2, Mask) &&
1779 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001780 Op<0>() = V1;
1781 Op<1>() = V2;
1782 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001783 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001784}
1785
1786ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001787 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001788 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001789: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1790 cast<VectorType>(Mask->getType())->getNumElements()),
1791 ShuffleVector,
1792 OperandTraits<ShuffleVectorInst>::op_begin(this),
1793 OperandTraits<ShuffleVectorInst>::operands(this),
1794 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001795 assert(isValidOperands(V1, V2, Mask) &&
1796 "Invalid shuffle vector instruction operands!");
1797
Gabor Greif2d3024d2008-05-26 21:33:52 +00001798 Op<0>() = V1;
1799 Op<1>() = V2;
1800 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001801 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001802}
1803
Mon P Wang25f01062008-11-10 04:46:22 +00001804bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001805 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001806 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001807 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001808 return false;
1809
Chris Lattner1dcb6542012-01-25 23:49:49 +00001810 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001811 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001812 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001813 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001814
1815 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001816 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1817 return true;
1818
Nate Begeman60a31c32010-08-13 00:16:46 +00001819 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001820 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001821 for (Value *Op : MV->operands()) {
1822 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001823 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001824 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001825 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001826 return false;
1827 }
1828 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001829 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001830 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001831
1832 if (const ConstantDataSequential *CDS =
1833 dyn_cast<ConstantDataSequential>(Mask)) {
1834 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1835 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1836 if (CDS->getElementAsInteger(i) >= V1Size*2)
1837 return false;
1838 return true;
1839 }
1840
1841 // The bitcode reader can create a place holder for a forward reference
1842 // used as the shuffle mask. When this occurs, the shuffle mask will
1843 // fall into this case and fail. To avoid this error, do this bit of
1844 // ugliness to allow such a mask pass.
1845 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1846 if (CE->getOpcode() == Instruction::UserOp1)
1847 return true;
1848
1849 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001850}
1851
Chris Lattnerf724e342008-03-02 05:28:33 +00001852/// getMaskValue - Return the index from the shuffle mask for the specified
1853/// output result. This is either -1 if the element is undef or a number less
1854/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001855int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1856 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1857 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001858 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001859 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001860 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001861 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001862 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001863}
1864
Chris Lattner1dcb6542012-01-25 23:49:49 +00001865/// getShuffleMask - Return the full mask for this instruction, where each
1866/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001867void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1868 SmallVectorImpl<int> &Result) {
1869 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001870
Chris Lattnercf129702012-01-26 02:51:13 +00001871 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001872 for (unsigned i = 0; i != NumElts; ++i)
1873 Result.push_back(CDS->getElementAsInteger(i));
1874 return;
1875 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001876 for (unsigned i = 0; i != NumElts; ++i) {
1877 Constant *C = Mask->getAggregateElement(i);
1878 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001879 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001880 }
1881}
1882
1883
Dan Gohman12fce772008-05-15 19:50:34 +00001884//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001885// InsertValueInst Class
1886//===----------------------------------------------------------------------===//
1887
Jay Foad57aa6362011-07-13 10:26:04 +00001888void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1889 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001890 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001891
1892 // There's no fundamental reason why we require at least one index
1893 // (other than weirdness with &*IdxBegin being invalid; see
1894 // getelementptr's init routine for example). But there's no
1895 // present need to support it.
1896 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1897
1898 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001899 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001900 Op<0>() = Agg;
1901 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001902
Jay Foad57aa6362011-07-13 10:26:04 +00001903 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001904 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001905}
1906
1907InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001908 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001909 OperandTraits<InsertValueInst>::op_begin(this), 2),
1910 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001911 Op<0>() = IVI.getOperand(0);
1912 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001913 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001914}
1915
1916//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001917// ExtractValueInst Class
1918//===----------------------------------------------------------------------===//
1919
Jay Foad57aa6362011-07-13 10:26:04 +00001920void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001921 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001922
Jay Foad57aa6362011-07-13 10:26:04 +00001923 // There's no fundamental reason why we require at least one index.
1924 // But there's no present need to support it.
1925 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001926
Jay Foad57aa6362011-07-13 10:26:04 +00001927 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001928 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001929}
1930
1931ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001932 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001933 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001934 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001935}
1936
Dan Gohman12fce772008-05-15 19:50:34 +00001937// getIndexedType - Returns the type of the element that would be extracted
1938// with an extractvalue instruction with the specified parameters.
1939//
1940// A null type is returned if the indices are invalid for the specified
1941// pointer type.
1942//
Chris Lattner229907c2011-07-18 04:54:35 +00001943Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001944 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001945 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001946 // We can't use CompositeType::indexValid(Index) here.
1947 // indexValid() always returns true for arrays because getelementptr allows
1948 // out-of-bounds indices. Since we don't allow those for extractvalue and
1949 // insertvalue we need to check array indexing manually.
1950 // Since the only other types we can index into are struct types it's just
1951 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001952 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001953 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001954 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001955 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001956 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001957 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001958 } else {
1959 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001960 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001961 }
1962
1963 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001964 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001965 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001966}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001967
1968//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001969// BinaryOperator Class
1970//===----------------------------------------------------------------------===//
1971
Chris Lattner2195fc42007-02-24 00:55:48 +00001972BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001973 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001974 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001975 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001976 OperandTraits<BinaryOperator>::op_begin(this),
1977 OperandTraits<BinaryOperator>::operands(this),
1978 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001979 Op<0>() = S1;
1980 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001981 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001982 setName(Name);
1983}
1984
1985BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001986 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001987 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001988 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001989 OperandTraits<BinaryOperator>::op_begin(this),
1990 OperandTraits<BinaryOperator>::operands(this),
1991 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001992 Op<0>() = S1;
1993 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001994 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001995 setName(Name);
1996}
1997
1998
1999void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002000 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002001 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002002 assert(LHS->getType() == RHS->getType() &&
2003 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002004#ifndef NDEBUG
2005 switch (iType) {
2006 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002007 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002008 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002009 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002011 "Tried to create an integer operation on a non-integer type!");
2012 break;
2013 case FAdd: case FSub:
2014 case FMul:
2015 assert(getType() == LHS->getType() &&
2016 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002017 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002018 "Tried to create a floating-point operation on a "
2019 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002020 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002021 case UDiv:
2022 case SDiv:
2023 assert(getType() == LHS->getType() &&
2024 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002025 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002026 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002027 "Incorrect operand type (not integer) for S/UDIV");
2028 break;
2029 case FDiv:
2030 assert(getType() == LHS->getType() &&
2031 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002032 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002033 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002034 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002035 case URem:
2036 case SRem:
2037 assert(getType() == LHS->getType() &&
2038 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002039 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002040 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002041 "Incorrect operand type (not integer) for S/UREM");
2042 break;
2043 case FRem:
2044 assert(getType() == LHS->getType() &&
2045 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002046 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002047 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002048 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002049 case Shl:
2050 case LShr:
2051 case AShr:
2052 assert(getType() == LHS->getType() &&
2053 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002054 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002055 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002056 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002057 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002058 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002059 case And: case Or:
2060 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002061 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002062 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002063 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002064 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002065 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002066 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002067 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002068 default:
2069 break;
2070 }
2071#endif
2072}
2073
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002074BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002075 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076 Instruction *InsertBefore) {
2077 assert(S1->getType() == S2->getType() &&
2078 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002079 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002080}
2081
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002082BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002083 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002085 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002086 InsertAtEnd->getInstList().push_back(Res);
2087 return Res;
2088}
2089
Dan Gohman5476cfd2009-08-12 16:23:25 +00002090BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002091 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002092 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002093 return new BinaryOperator(Instruction::Sub,
2094 zero, Op,
2095 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002096}
2097
Dan Gohman5476cfd2009-08-12 16:23:25 +00002098BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002099 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002100 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002101 return new BinaryOperator(Instruction::Sub,
2102 zero, Op,
2103 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104}
2105
Dan Gohman4ab44202009-12-18 02:58:50 +00002106BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2107 Instruction *InsertBefore) {
2108 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2109 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2110}
2111
2112BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2113 BasicBlock *InsertAtEnd) {
2114 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2115 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2116}
2117
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002118BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2119 Instruction *InsertBefore) {
2120 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2121 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2122}
2123
2124BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2125 BasicBlock *InsertAtEnd) {
2126 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2127 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2128}
2129
Dan Gohman5476cfd2009-08-12 16:23:25 +00002130BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002131 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002132 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002133 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002134 Op->getType(), Name, InsertBefore);
2135}
2136
Dan Gohman5476cfd2009-08-12 16:23:25 +00002137BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002138 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002139 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002140 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002141 Op->getType(), Name, InsertAtEnd);
2142}
2143
Dan Gohman5476cfd2009-08-12 16:23:25 +00002144BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002145 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002146 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002147 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002148 Op->getType(), Name, InsertBefore);
2149}
2150
Dan Gohman5476cfd2009-08-12 16:23:25 +00002151BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002153 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002154 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002155 Op->getType(), Name, InsertAtEnd);
2156}
2157
2158
2159// isConstantAllOnes - Helper function for several functions below
2160static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002161 if (const Constant *C = dyn_cast<Constant>(V))
2162 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002163 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002164}
2165
Owen Andersonbb2501b2009-07-13 22:18:28 +00002166bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002167 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2168 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002169 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002170 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002171 return false;
2172}
2173
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002174bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002175 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2176 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002177 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002178 if (!IgnoreZeroSign)
2179 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2180 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2181 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002182 return false;
2183}
2184
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002185bool BinaryOperator::isNot(const Value *V) {
2186 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2187 return (Bop->getOpcode() == Instruction::Xor &&
2188 (isConstantAllOnes(Bop->getOperand(1)) ||
2189 isConstantAllOnes(Bop->getOperand(0))));
2190 return false;
2191}
2192
Chris Lattner2c7d1772005-04-24 07:28:37 +00002193Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002194 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002195}
2196
Chris Lattner2c7d1772005-04-24 07:28:37 +00002197const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2198 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002199}
2200
Dan Gohmana5b96452009-06-04 22:49:04 +00002201Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002202 return cast<BinaryOperator>(BinOp)->getOperand(1);
2203}
2204
2205const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2206 return getFNegArgument(const_cast<Value*>(BinOp));
2207}
2208
Chris Lattner2c7d1772005-04-24 07:28:37 +00002209Value *BinaryOperator::getNotArgument(Value *BinOp) {
2210 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2211 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2212 Value *Op0 = BO->getOperand(0);
2213 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002214 if (isConstantAllOnes(Op0)) return Op1;
2215
2216 assert(isConstantAllOnes(Op1));
2217 return Op0;
2218}
2219
Chris Lattner2c7d1772005-04-24 07:28:37 +00002220const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2221 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002222}
2223
2224
2225// swapOperands - Exchange the two operands to this instruction. This
2226// instruction is safe to use on any binary instruction and does not
2227// modify the semantics of the instruction. If the instruction is
2228// order dependent (SetLT f.e.) the opcode is changed.
2229//
2230bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002231 if (!isCommutative())
2232 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002233 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002234 return false;
2235}
2236
Sanjay Patela982d992014-09-03 01:06:50 +00002237
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002238//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002239// FPMathOperator Class
2240//===----------------------------------------------------------------------===//
2241
2242/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2243/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002244/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002245float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002246 const MDNode *MD =
2247 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002248 if (!MD)
2249 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002250 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002251 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002252}
2253
2254
2255//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002256// CastInst Class
2257//===----------------------------------------------------------------------===//
2258
David Blaikie3a15e142011-12-01 08:00:17 +00002259void CastInst::anchor() {}
2260
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261// Just determine if this cast only deals with integral->integral conversion.
2262bool CastInst::isIntegerCast() const {
2263 switch (getOpcode()) {
2264 default: return false;
2265 case Instruction::ZExt:
2266 case Instruction::SExt:
2267 case Instruction::Trunc:
2268 return true;
2269 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002270 return getOperand(0)->getType()->isIntegerTy() &&
2271 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002273}
2274
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275bool CastInst::isLosslessCast() const {
2276 // Only BitCast can be lossless, exit fast if we're not BitCast
2277 if (getOpcode() != Instruction::BitCast)
2278 return false;
2279
2280 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002281 Type *SrcTy = getOperand(0)->getType();
2282 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 if (SrcTy == DstTy)
2284 return true;
2285
Reid Spencer8d9336d2006-12-31 05:26:44 +00002286 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002287 if (SrcTy->isPointerTy())
2288 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 return false; // Other types have no identity values
2290}
2291
2292/// This function determines if the CastInst does not require any bits to be
2293/// changed in order to effect the cast. Essentially, it identifies cases where
2294/// no code gen is necessary for the cast, hence the name no-op cast. For
2295/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002296/// # bitcast i32* %x to i8*
2297/// # bitcast <2 x i32> %x to <4 x i16>
2298/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002299/// @brief Determine if the described cast is a no-op.
2300bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002301 Type *SrcTy,
2302 Type *DestTy,
2303 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002304 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002305 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 case Instruction::Trunc:
2307 case Instruction::ZExt:
2308 case Instruction::SExt:
2309 case Instruction::FPTrunc:
2310 case Instruction::FPExt:
2311 case Instruction::UIToFP:
2312 case Instruction::SIToFP:
2313 case Instruction::FPToUI:
2314 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002315 case Instruction::AddrSpaceCast:
2316 // TODO: Target informations may give a more accurate answer here.
2317 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318 case Instruction::BitCast:
2319 return true; // BitCast never modifies bits.
2320 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002321 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002322 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002324 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002325 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 }
2327}
2328
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002329/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002330bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002331 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2332}
2333
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002334bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002335 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002336 if (getOpcode() == Instruction::PtrToInt)
2337 PtrOpTy = getOperand(0)->getType();
2338 else if (getOpcode() == Instruction::IntToPtr)
2339 PtrOpTy = getType();
2340
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002341 Type *IntPtrTy =
2342 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002343
2344 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2345}
2346
2347/// This function determines if a pair of casts can be eliminated and what
2348/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349/// instructions like this:
2350/// * %F = firstOpcode SrcTy %x to MidTy
2351/// * %S = secondOpcode MidTy %F to DstTy
2352/// The function returns a resultOpcode so these two casts can be replaced with:
2353/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002354/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355unsigned CastInst::isEliminableCastPair(
2356 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002357 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2358 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 // Define the 144 possibilities for these two cast instructions. The values
2360 // in this matrix determine what to do in a given situation and select the
2361 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002362 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 // the following cast properties:
2364 //
2365 // Size Compare Source Destination
2366 // Operator Src ? Size Type Sign Type Sign
2367 // -------- ------------ ------------------- ---------------------
2368 // TRUNC > Integer Any Integral Any
2369 // ZEXT < Integral Unsigned Integer Any
2370 // SEXT < Integral Signed Integer Any
2371 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002372 // FPTOSI n/a FloatPt n/a Integral Signed
2373 // UITOFP n/a Integral Unsigned FloatPt n/a
2374 // SITOFP n/a Integral Signed FloatPt n/a
2375 // FPTRUNC > FloatPt n/a FloatPt n/a
2376 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 // PTRTOINT n/a Pointer n/a Integral Unsigned
2378 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002379 // BITCAST = FirstClass n/a FirstClass n/a
2380 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002381 //
2382 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002383 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2384 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002385 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002386 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002387 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002388 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002389 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2391 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002392 // T F F U S F F P I B A -+
2393 // R Z S P P I I T P 2 N T S |
2394 // U E E 2 2 2 2 R E I T C C +- secondOp
2395 // N X X U S F F N X N 2 V V |
2396 // C T T I I P P C T T P T T -+
2397 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002398 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2400 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2401 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2402 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2403 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002404 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2406 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2407 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2408 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2409 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002411
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002412 // TODO: This logic could be encoded into the table above and handled in the
2413 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002414 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002415 // merging. However, any pair of bitcasts are allowed.
2416 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2417 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2418 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002419
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002420 // Check if any of the casts convert scalars <-> vectors.
2421 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2422 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2423 if (!AreBothBitcasts)
2424 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425
2426 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2427 [secondOp-Instruction::CastOpsBegin];
2428 switch (ElimCase) {
2429 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002430 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431 return 0;
2432 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002433 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 return firstOp;
2435 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002436 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 return secondOp;
2438 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002440 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002441 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002442 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 return firstOp;
2444 return 0;
2445 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002446 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002447 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002448 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 return firstOp;
2450 return 0;
2451 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002452 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002453 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002454 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 return secondOp;
2456 return 0;
2457 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002458 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002459 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002460 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 return secondOp;
2462 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002463 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002464 // Cannot simplify if address spaces are different!
2465 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2466 return 0;
2467
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002468 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002469 // We can still fold this without knowing the actual sizes as long we
2470 // know that the intermediate pointer is the largest possible
2471 // pointer size.
2472 // FIXME: Is this always true?
2473 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002474 return Instruction::BitCast;
2475
2476 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002477 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002478 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002479 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 if (MidSize >= PtrSize)
2481 return Instruction::BitCast;
2482 return 0;
2483 }
2484 case 8: {
2485 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2486 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2487 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002488 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2489 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 if (SrcSize == DstSize)
2491 return Instruction::BitCast;
2492 else if (SrcSize < DstSize)
2493 return firstOp;
2494 return secondOp;
2495 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 case 9:
2497 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 return Instruction::ZExt;
2499 case 10:
2500 // fpext followed by ftrunc is allowed if the bit size returned to is
2501 // the same as the original, in which case its just a bitcast
2502 if (SrcTy == DstTy)
2503 return Instruction::BitCast;
2504 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002505 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002507 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002508 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002509 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002510 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2511 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 if (SrcSize <= PtrSize && SrcSize == DstSize)
2513 return Instruction::BitCast;
2514 return 0;
2515 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002516 case 12: {
2517 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2518 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2519 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2520 return Instruction::AddrSpaceCast;
2521 return Instruction::BitCast;
2522 }
2523 case 13:
2524 // FIXME: this state can be merged with (1), but the following assert
2525 // is useful to check the correcteness of the sequence due to semantic
2526 // change of bitcast.
2527 assert(
2528 SrcTy->isPtrOrPtrVectorTy() &&
2529 MidTy->isPtrOrPtrVectorTy() &&
2530 DstTy->isPtrOrPtrVectorTy() &&
2531 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2532 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2533 "Illegal addrspacecast, bitcast sequence!");
2534 // Allowed, use first cast's opcode
2535 return firstOp;
2536 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002537 // bitcast, addrspacecast -> addrspacecast if the element type of
2538 // bitcast's source is the same as that of addrspacecast's destination.
2539 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2540 return Instruction::AddrSpaceCast;
2541 return 0;
2542
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002543 case 15:
2544 // FIXME: this state can be merged with (1), but the following assert
2545 // is useful to check the correcteness of the sequence due to semantic
2546 // change of bitcast.
2547 assert(
2548 SrcTy->isIntOrIntVectorTy() &&
2549 MidTy->isPtrOrPtrVectorTy() &&
2550 DstTy->isPtrOrPtrVectorTy() &&
2551 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2552 "Illegal inttoptr, bitcast sequence!");
2553 // Allowed, use first cast's opcode
2554 return firstOp;
2555 case 16:
2556 // FIXME: this state can be merged with (2), but the following assert
2557 // is useful to check the correcteness of the sequence due to semantic
2558 // change of bitcast.
2559 assert(
2560 SrcTy->isPtrOrPtrVectorTy() &&
2561 MidTy->isPtrOrPtrVectorTy() &&
2562 DstTy->isIntOrIntVectorTy() &&
2563 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2564 "Illegal bitcast, ptrtoint sequence!");
2565 // Allowed, use second cast's opcode
2566 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002567 case 17:
2568 // (sitofp (zext x)) -> (uitofp x)
2569 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002571 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002573 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574 default:
Craig Topperc514b542012-02-05 22:14:15 +00002575 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577}
2578
Chris Lattner229907c2011-07-18 04:54:35 +00002579CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002581 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582 // Construct and return the appropriate CastInst subclass
2583 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002584 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2585 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2586 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2587 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2588 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2589 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2590 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2591 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2592 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2593 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2594 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2595 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2596 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2597 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599}
2600
Chris Lattner229907c2011-07-18 04:54:35 +00002601CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002602 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002603 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 // Construct and return the appropriate CastInst subclass
2605 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002606 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2607 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2608 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2609 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2610 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2611 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2612 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2613 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2614 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2615 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2616 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2617 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2618 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2619 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621}
2622
Chris Lattner229907c2011-07-18 04:54:35 +00002623CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002625 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002626 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002627 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2628 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002629}
2630
Chris Lattner229907c2011-07-18 04:54:35 +00002631CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002632 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002633 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002634 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002635 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2636 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002637}
2638
Chris Lattner229907c2011-07-18 04:54:35 +00002639CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002641 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002642 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002643 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2644 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002645}
2646
Chris Lattner229907c2011-07-18 04:54:35 +00002647CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002648 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002649 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002650 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002651 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2652 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002653}
2654
Chris Lattner229907c2011-07-18 04:54:35 +00002655CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002657 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002658 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002659 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2660 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002661}
2662
Chris Lattner229907c2011-07-18 04:54:35 +00002663CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002665 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002666 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002667 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2668 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002669}
2670
Chris Lattner229907c2011-07-18 04:54:35 +00002671CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002672 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002673 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002674 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2675 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2676 "Invalid cast");
2677 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002678 assert((!Ty->isVectorTy() ||
2679 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002680 "Invalid cast");
2681
Matt Arsenault065ced92013-07-31 00:17:33 +00002682 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002683 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002684
Matt Arsenault740980e2014-07-14 17:24:35 +00002685 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002686}
2687
2688/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002689CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2690 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002691 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002692 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2693 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002694 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002695 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002696 assert((!Ty->isVectorTy() ||
2697 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002698 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002699
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002700 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002701 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002702
Matt Arsenault740980e2014-07-14 17:24:35 +00002703 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2704}
2705
2706CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2707 Value *S, Type *Ty,
2708 const Twine &Name,
2709 BasicBlock *InsertAtEnd) {
2710 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2711 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2712
2713 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2714 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2715
2716 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2717}
2718
2719CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2720 Value *S, Type *Ty,
2721 const Twine &Name,
2722 Instruction *InsertBefore) {
2723 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2724 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2725
2726 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002727 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2728
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002729 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002730}
2731
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002732CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2733 const Twine &Name,
2734 Instruction *InsertBefore) {
2735 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2736 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2737 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2738 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2739
2740 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2741}
2742
Matt Arsenault740980e2014-07-14 17:24:35 +00002743CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002744 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002745 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002746 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002747 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002748 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2749 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002750 Instruction::CastOps opcode =
2751 (SrcBits == DstBits ? Instruction::BitCast :
2752 (SrcBits > DstBits ? Instruction::Trunc :
2753 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002754 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002755}
2756
Chris Lattner229907c2011-07-18 04:54:35 +00002757CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002758 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002759 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002760 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002761 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002762 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2763 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002764 Instruction::CastOps opcode =
2765 (SrcBits == DstBits ? Instruction::BitCast :
2766 (SrcBits > DstBits ? Instruction::Trunc :
2767 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002768 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002769}
2770
Chris Lattner229907c2011-07-18 04:54:35 +00002771CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002772 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002773 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002774 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002775 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002776 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2777 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002778 Instruction::CastOps opcode =
2779 (SrcBits == DstBits ? Instruction::BitCast :
2780 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002781 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002782}
2783
Chris Lattner229907c2011-07-18 04:54:35 +00002784CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002785 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002786 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002787 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002788 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002789 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2790 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002791 Instruction::CastOps opcode =
2792 (SrcBits == DstBits ? Instruction::BitCast :
2793 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002794 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002795}
2796
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002797// Check whether it is valid to call getCastOpcode for these types.
2798// This routine must be kept in sync with getCastOpcode.
2799bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2800 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2801 return false;
2802
2803 if (SrcTy == DestTy)
2804 return true;
2805
2806 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2807 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2808 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2809 // An element by element cast. Valid if casting the elements is valid.
2810 SrcTy = SrcVecTy->getElementType();
2811 DestTy = DestVecTy->getElementType();
2812 }
2813
2814 // Get the bit sizes, we'll need these
2815 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2816 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2817
2818 // Run through the possibilities ...
2819 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002820 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002821 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002822 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002823 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002824 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002825 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002826 // Casting from something else
2827 return SrcTy->isPointerTy();
2828 }
2829 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2830 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002831 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002832 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002833 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002834 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002835 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002836 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002837 return false;
2838 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002839 if (DestTy->isVectorTy()) // Casting to vector
2840 return DestBits == SrcBits;
2841 if (DestTy->isPointerTy()) { // Casting to pointer
2842 if (SrcTy->isPointerTy()) // Casting from pointer
2843 return true;
2844 return SrcTy->isIntegerTy(); // Casting from integral
2845 }
2846 if (DestTy->isX86_MMXTy()) {
2847 if (SrcTy->isVectorTy())
2848 return DestBits == SrcBits; // 64-bit vector to MMX
2849 return false;
2850 } // Casting to something else
2851 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002852}
2853
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002854bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2855 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2856 return false;
2857
2858 if (SrcTy == DestTy)
2859 return true;
2860
2861 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2862 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2863 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2864 // An element by element cast. Valid if casting the elements is valid.
2865 SrcTy = SrcVecTy->getElementType();
2866 DestTy = DestVecTy->getElementType();
2867 }
2868 }
2869 }
2870
2871 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2872 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2873 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2874 }
2875 }
2876
2877 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2878 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2879
2880 // Could still have vectors of pointers if the number of elements doesn't
2881 // match
2882 if (SrcBits == 0 || DestBits == 0)
2883 return false;
2884
2885 if (SrcBits != DestBits)
2886 return false;
2887
2888 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2889 return false;
2890
2891 return true;
2892}
2893
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002894bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002895 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002896 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2897 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002898 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002899 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2900 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002901 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002902
2903 return isBitCastable(SrcTy, DestTy);
2904}
2905
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002906// Provide a way to get a "cast" where the cast opcode is inferred from the
2907// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002908// logic in the castIsValid function below. This axiom should hold:
2909// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2910// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002912// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002914CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002915 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2916 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917
Duncan Sands55e50902008-01-06 10:12:28 +00002918 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2919 "Only first class types are castable!");
2920
Duncan Sandsa8514532011-05-18 07:13:41 +00002921 if (SrcTy == DestTy)
2922 return BitCast;
2923
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002924 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002925 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2926 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002927 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2928 // An element by element cast. Find the appropriate opcode based on the
2929 // element types.
2930 SrcTy = SrcVecTy->getElementType();
2931 DestTy = DestVecTy->getElementType();
2932 }
2933
2934 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002935 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2936 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002937
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002939 if (DestTy->isIntegerTy()) { // Casting to integral
2940 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 if (DestBits < SrcBits)
2942 return Trunc; // int -> smaller int
2943 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002944 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 return SExt; // signed -> SEXT
2946 else
2947 return ZExt; // unsigned -> ZEXT
2948 } else {
2949 return BitCast; // Same size, No-op cast
2950 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002951 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002952 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953 return FPToSI; // FP -> sint
2954 else
2955 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002956 } else if (SrcTy->isVectorTy()) {
2957 assert(DestBits == SrcBits &&
2958 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959 return BitCast; // Same size, no-op cast
2960 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002961 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002962 "Casting from a value that is not first-class type");
2963 return PtrToInt; // ptr -> int
2964 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002965 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2966 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002967 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002968 return SIToFP; // sint -> FP
2969 else
2970 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002971 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002972 if (DestBits < SrcBits) {
2973 return FPTrunc; // FP -> smaller FP
2974 } else if (DestBits > SrcBits) {
2975 return FPExt; // FP -> larger FP
2976 } else {
2977 return BitCast; // same size, no-op cast
2978 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002979 } else if (SrcTy->isVectorTy()) {
2980 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002981 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002982 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002984 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002985 } else if (DestTy->isVectorTy()) {
2986 assert(DestBits == SrcBits &&
2987 "Illegal cast to vector (wrong type or size)");
2988 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002989 } else if (DestTy->isPointerTy()) {
2990 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002991 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2992 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002993 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002994 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002997 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002998 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002999 if (SrcTy->isVectorTy()) {
3000 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003001 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003002 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003003 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003004 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003005 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006}
3007
3008//===----------------------------------------------------------------------===//
3009// CastInst SubClass Constructors
3010//===----------------------------------------------------------------------===//
3011
3012/// Check that the construction parameters for a CastInst are correct. This
3013/// could be broken out into the separate constructors but it is useful to have
3014/// it in one place and to eliminate the redundant code for getting the sizes
3015/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003016bool
Chris Lattner229907c2011-07-18 04:54:35 +00003017CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003018
3019 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003020 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003021
Chris Lattner37bc78a2010-01-26 21:51:43 +00003022 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3023 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 return false;
3025
3026 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003027 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3028 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029
Duncan Sands7f646562011-05-18 09:21:57 +00003030 // If these are vector types, get the lengths of the vectors (using zero for
3031 // scalar types means that checking that vector lengths match also checks that
3032 // scalars are not being converted to vectors or vectors to scalars).
3033 unsigned SrcLength = SrcTy->isVectorTy() ?
3034 cast<VectorType>(SrcTy)->getNumElements() : 0;
3035 unsigned DstLength = DstTy->isVectorTy() ?
3036 cast<VectorType>(DstTy)->getNumElements() : 0;
3037
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 // Switch on the opcode provided
3039 switch (op) {
3040 default: return false; // This is an input error
3041 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003042 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3043 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003044 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003045 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3046 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003048 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3049 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003051 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3052 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003054 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3055 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003058 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3059 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003062 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3063 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003065 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003066 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003067 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3068 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3069 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003070 return SrcTy->getScalarType()->isPointerTy() &&
3071 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003073 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003074 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003075 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3076 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3077 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003078 return SrcTy->getScalarType()->isIntegerTy() &&
3079 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003080 case Instruction::BitCast: {
3081 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3082 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3083
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003084 // BitCast implies a no-op cast of type only. No bits change.
3085 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003086 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003087 return false;
3088
Alp Tokerf907b892013-12-05 05:44:44 +00003089 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003090 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003091 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003092 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3093
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003094 // If both are pointers then the address spaces must match.
3095 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3096 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003097
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003098 // A vector of pointers must have the same number of elements.
3099 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3100 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3101 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3102
3103 return false;
3104 }
3105
3106 return true;
3107 }
3108 case Instruction::AddrSpaceCast: {
3109 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3110 if (!SrcPtrTy)
3111 return false;
3112
3113 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3114 if (!DstPtrTy)
3115 return false;
3116
3117 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3118 return false;
3119
3120 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3121 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3122 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3123
3124 return false;
3125 }
3126
3127 return true;
3128 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129 }
3130}
3131
3132TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003133 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003134) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003135 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136}
3137
3138TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003139 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003141 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142}
3143
3144ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003145 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003147 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003148}
3149
3150ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003151 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003153 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154}
3155SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003156 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159}
3160
Jeff Cohencc08c832006-12-02 02:22:01 +00003161SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003162 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003165}
3166
3167FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003168 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003170 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171}
3172
3173FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003174 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003175) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003176 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003177}
3178
3179FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003180 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003182 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003183}
3184
3185FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003186 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189}
3190
3191UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003192 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195}
3196
3197UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003198 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201}
3202
3203SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003204 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207}
3208
3209SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003210 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213}
3214
3215FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003216 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003217) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219}
3220
3221FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003222 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225}
3226
3227FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003228 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003229) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231}
3232
3233FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003234 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237}
3238
3239PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003240 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243}
3244
3245PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003246 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003249}
3250
3251IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003252 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003254 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003255}
3256
3257IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003258 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003260 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003261}
3262
3263BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003264 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003265) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003266 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003267}
3268
3269BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003270 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003271) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003272 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003273}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003274
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003275AddrSpaceCastInst::AddrSpaceCastInst(
3276 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3277) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3278 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3279}
3280
3281AddrSpaceCastInst::AddrSpaceCastInst(
3282 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3283) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3284 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3285}
3286
Chris Lattnerf16dc002006-09-17 19:29:56 +00003287//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003288// CmpInst Classes
3289//===----------------------------------------------------------------------===//
3290
Craig Topper3186c012012-09-23 02:12:10 +00003291void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003292
Craig Topper1c3f2832015-12-15 06:11:33 +00003293CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3294 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003295 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003296 OperandTraits<CmpInst>::op_begin(this),
3297 OperandTraits<CmpInst>::operands(this),
3298 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003299 Op<0>() = LHS;
3300 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003301 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003302 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003303}
Gabor Greiff6caff662008-05-10 08:32:32 +00003304
Craig Topper1c3f2832015-12-15 06:11:33 +00003305CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3306 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003307 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003308 OperandTraits<CmpInst>::op_begin(this),
3309 OperandTraits<CmpInst>::operands(this),
3310 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003311 Op<0>() = LHS;
3312 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003313 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003314 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003315}
3316
3317CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003318CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003319 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003320 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321 if (InsertBefore)
3322 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3323 S1, S2, Name);
3324 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003325 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003327 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328
3329 if (InsertBefore)
3330 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3331 S1, S2, Name);
3332 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003333 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003335}
3336
3337CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003338CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003339 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003340 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003341 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3342 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003343 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003344 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3345 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003346}
3347
3348void CmpInst::swapOperands() {
3349 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3350 IC->swapOperands();
3351 else
3352 cast<FCmpInst>(this)->swapOperands();
3353}
3354
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003355bool CmpInst::isCommutative() const {
3356 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003357 return IC->isCommutative();
3358 return cast<FCmpInst>(this)->isCommutative();
3359}
3360
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003361bool CmpInst::isEquality() const {
3362 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003363 return IC->isEquality();
3364 return cast<FCmpInst>(this)->isEquality();
3365}
3366
3367
Dan Gohman4e724382008-05-31 02:47:54 +00003368CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003369 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003370 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003371 case ICMP_EQ: return ICMP_NE;
3372 case ICMP_NE: return ICMP_EQ;
3373 case ICMP_UGT: return ICMP_ULE;
3374 case ICMP_ULT: return ICMP_UGE;
3375 case ICMP_UGE: return ICMP_ULT;
3376 case ICMP_ULE: return ICMP_UGT;
3377 case ICMP_SGT: return ICMP_SLE;
3378 case ICMP_SLT: return ICMP_SGE;
3379 case ICMP_SGE: return ICMP_SLT;
3380 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003381
Dan Gohman4e724382008-05-31 02:47:54 +00003382 case FCMP_OEQ: return FCMP_UNE;
3383 case FCMP_ONE: return FCMP_UEQ;
3384 case FCMP_OGT: return FCMP_ULE;
3385 case FCMP_OLT: return FCMP_UGE;
3386 case FCMP_OGE: return FCMP_ULT;
3387 case FCMP_OLE: return FCMP_UGT;
3388 case FCMP_UEQ: return FCMP_ONE;
3389 case FCMP_UNE: return FCMP_OEQ;
3390 case FCMP_UGT: return FCMP_OLE;
3391 case FCMP_ULT: return FCMP_OGE;
3392 case FCMP_UGE: return FCMP_OLT;
3393 case FCMP_ULE: return FCMP_OGT;
3394 case FCMP_ORD: return FCMP_UNO;
3395 case FCMP_UNO: return FCMP_ORD;
3396 case FCMP_TRUE: return FCMP_FALSE;
3397 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003398 }
3399}
3400
Pete Cooper1d078692015-12-14 20:29:16 +00003401void ICmpInst::anchor() {}
3402
Reid Spencer266e42b2006-12-23 06:05:41 +00003403ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3404 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003405 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003406 case ICMP_EQ: case ICMP_NE:
3407 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3408 return pred;
3409 case ICMP_UGT: return ICMP_SGT;
3410 case ICMP_ULT: return ICMP_SLT;
3411 case ICMP_UGE: return ICMP_SGE;
3412 case ICMP_ULE: return ICMP_SLE;
3413 }
3414}
3415
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003416ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3417 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003418 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003419 case ICMP_EQ: case ICMP_NE:
3420 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3421 return pred;
3422 case ICMP_SGT: return ICMP_UGT;
3423 case ICMP_SLT: return ICMP_ULT;
3424 case ICMP_SGE: return ICMP_UGE;
3425 case ICMP_SLE: return ICMP_ULE;
3426 }
3427}
3428
Reid Spencer0286bc12007-02-28 22:00:54 +00003429/// Initialize a set of values that all satisfy the condition with C.
3430///
3431ConstantRange
3432ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3433 APInt Lower(C);
3434 APInt Upper(C);
3435 uint32_t BitWidth = C.getBitWidth();
3436 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003437 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003438 case ICmpInst::ICMP_EQ: ++Upper; break;
3439 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003440 case ICmpInst::ICMP_ULT:
3441 Lower = APInt::getMinValue(BitWidth);
3442 // Check for an empty-set condition.
3443 if (Lower == Upper)
3444 return ConstantRange(BitWidth, /*isFullSet=*/false);
3445 break;
3446 case ICmpInst::ICMP_SLT:
3447 Lower = APInt::getSignedMinValue(BitWidth);
3448 // Check for an empty-set condition.
3449 if (Lower == Upper)
3450 return ConstantRange(BitWidth, /*isFullSet=*/false);
3451 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003452 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003453 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003454 // Check for an empty-set condition.
3455 if (Lower == Upper)
3456 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003457 break;
3458 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003459 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003460 // Check for an empty-set condition.
3461 if (Lower == Upper)
3462 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003463 break;
3464 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003465 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003466 // Check for a full-set condition.
3467 if (Lower == Upper)
3468 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003469 break;
3470 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003471 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003472 // Check for a full-set condition.
3473 if (Lower == Upper)
3474 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003475 break;
3476 case ICmpInst::ICMP_UGE:
3477 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003478 // Check for a full-set condition.
3479 if (Lower == Upper)
3480 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003481 break;
3482 case ICmpInst::ICMP_SGE:
3483 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003484 // Check for a full-set condition.
3485 if (Lower == Upper)
3486 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003487 break;
3488 }
3489 return ConstantRange(Lower, Upper);
3490}
3491
Dan Gohman4e724382008-05-31 02:47:54 +00003492CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003493 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003494 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003495 case ICMP_EQ: case ICMP_NE:
3496 return pred;
3497 case ICMP_SGT: return ICMP_SLT;
3498 case ICMP_SLT: return ICMP_SGT;
3499 case ICMP_SGE: return ICMP_SLE;
3500 case ICMP_SLE: return ICMP_SGE;
3501 case ICMP_UGT: return ICMP_ULT;
3502 case ICMP_ULT: return ICMP_UGT;
3503 case ICMP_UGE: return ICMP_ULE;
3504 case ICMP_ULE: return ICMP_UGE;
3505
Reid Spencerd9436b62006-11-20 01:22:35 +00003506 case FCMP_FALSE: case FCMP_TRUE:
3507 case FCMP_OEQ: case FCMP_ONE:
3508 case FCMP_UEQ: case FCMP_UNE:
3509 case FCMP_ORD: case FCMP_UNO:
3510 return pred;
3511 case FCMP_OGT: return FCMP_OLT;
3512 case FCMP_OLT: return FCMP_OGT;
3513 case FCMP_OGE: return FCMP_OLE;
3514 case FCMP_OLE: return FCMP_OGE;
3515 case FCMP_UGT: return FCMP_ULT;
3516 case FCMP_ULT: return FCMP_UGT;
3517 case FCMP_UGE: return FCMP_ULE;
3518 case FCMP_ULE: return FCMP_UGE;
3519 }
3520}
3521
Sanjoy Das6e78b172015-10-22 19:57:34 +00003522CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3523 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3524
3525 switch (pred) {
3526 default:
3527 llvm_unreachable("Unknown predicate!");
3528 case CmpInst::ICMP_ULT:
3529 return CmpInst::ICMP_SLT;
3530 case CmpInst::ICMP_ULE:
3531 return CmpInst::ICMP_SLE;
3532 case CmpInst::ICMP_UGT:
3533 return CmpInst::ICMP_SGT;
3534 case CmpInst::ICMP_UGE:
3535 return CmpInst::ICMP_SGE;
3536 }
3537}
3538
Craig Topper1c3f2832015-12-15 06:11:33 +00003539bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003540 switch (predicate) {
3541 default: return false;
3542 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3543 case ICmpInst::ICMP_UGE: return true;
3544 }
3545}
3546
Craig Topper1c3f2832015-12-15 06:11:33 +00003547bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003548 switch (predicate) {
3549 default: return false;
3550 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3551 case ICmpInst::ICMP_SGE: return true;
3552 }
3553}
3554
Craig Topper1c3f2832015-12-15 06:11:33 +00003555bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003556 switch (predicate) {
3557 default: return false;
3558 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3559 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3560 case FCmpInst::FCMP_ORD: return true;
3561 }
3562}
3563
Craig Topper1c3f2832015-12-15 06:11:33 +00003564bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003565 switch (predicate) {
3566 default: return false;
3567 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3568 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3569 case FCmpInst::FCMP_UNO: return true;
3570 }
3571}
3572
Craig Topper1c3f2832015-12-15 06:11:33 +00003573bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003574 switch(predicate) {
3575 default: return false;
3576 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3577 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3578 }
3579}
3580
Craig Topper1c3f2832015-12-15 06:11:33 +00003581bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003582 switch(predicate) {
3583 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3584 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3585 default: return false;
3586 }
3587}
3588
Chad Rosier99bc4802016-04-21 16:18:02 +00003589bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003590 // If the predicates match, then we know the first condition implies the
3591 // second is true.
3592 if (Pred1 == Pred2)
3593 return true;
3594
3595 switch (Pred1) {
3596 default:
3597 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003598 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003599 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003600 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3601 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003602 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3603 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3604 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3605 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3606 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3607 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3608 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3609 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003610 }
3611 return false;
3612}
3613
Chad Rosier99bc4802016-04-21 16:18:02 +00003614bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003615 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003616}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003617
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003618//===----------------------------------------------------------------------===//
3619// SwitchInst Implementation
3620//===----------------------------------------------------------------------===//
3621
Chris Lattnerbaf00152010-11-17 05:41:46 +00003622void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3623 assert(Value && Default && NumReserved);
3624 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003625 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003626 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003627
Pete Coopereb31b682015-05-21 22:48:54 +00003628 Op<0>() = Value;
3629 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003630}
3631
Chris Lattner2195fc42007-02-24 00:55:48 +00003632/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3633/// switch on and a default destination. The number of additional cases can
3634/// be specified here to make memory allocation more efficient. This
3635/// constructor can also autoinsert before another instruction.
3636SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3637 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003638 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003639 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003640 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003641}
3642
3643/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3644/// switch on and a default destination. The number of additional cases can
3645/// be specified here to make memory allocation more efficient. This
3646/// constructor also autoinserts at the end of the specified BasicBlock.
3647SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3648 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003649 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003650 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003651 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003652}
3653
Misha Brukmanb1c93172005-04-21 23:48:37 +00003654SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003655 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003656 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003657 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003658 Use *OL = getOperandList();
3659 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003660 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003661 OL[i] = InOL[i];
3662 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003663 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003664 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003665}
3666
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003667
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003668/// addCase - Add an entry to the switch instruction...
3669///
Chris Lattner47ac1872005-02-24 05:32:09 +00003670void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003671 unsigned NewCaseIdx = getNumCases();
3672 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003673 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003674 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003675 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003676 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003677 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003678 CaseIt Case(this, NewCaseIdx);
3679 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003680 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003681}
3682
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003683/// removeCase - This method removes the specified case and its successor
3684/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003685void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003686 unsigned idx = i.getCaseIndex();
3687
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003688 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003689
3690 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003691 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003692
Jay Foad14277722011-02-01 09:22:34 +00003693 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003694 if (2 + (idx + 1) * 2 != NumOps) {
3695 OL[2 + idx * 2] = OL[NumOps - 2];
3696 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003697 }
3698
3699 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003700 OL[NumOps-2].set(nullptr);
3701 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003702 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003703}
3704
Jay Foade98f29d2011-04-01 08:00:58 +00003705/// growOperands - grow operands - This grows the operand list in response
3706/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003707///
Jay Foade98f29d2011-04-01 08:00:58 +00003708void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003709 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003710 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003711
3712 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003713 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003714}
3715
3716
3717BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3718 return getSuccessor(idx);
3719}
3720unsigned SwitchInst::getNumSuccessorsV() const {
3721 return getNumSuccessors();
3722}
3723void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3724 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003725}
Chris Lattnerf22be932004-10-15 23:52:53 +00003726
Chris Lattner3ed871f2009-10-27 19:13:16 +00003727//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003728// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003729//===----------------------------------------------------------------------===//
3730
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003731void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003732 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003733 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003734 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003735 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003736 allocHungoffUses(ReservedSpace);
3737
Pete Coopereb31b682015-05-21 22:48:54 +00003738 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739}
3740
3741
Jay Foade98f29d2011-04-01 08:00:58 +00003742/// growOperands - grow operands - This grows the operand list in response
3743/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003744///
Jay Foade98f29d2011-04-01 08:00:58 +00003745void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003746 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003747 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003748
3749 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003750 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751}
3752
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003753IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3754 Instruction *InsertBefore)
3755: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003756 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003757 init(Address, NumCases);
3758}
3759
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003760IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3761 BasicBlock *InsertAtEnd)
3762: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003763 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003764 init(Address, NumCases);
3765}
3766
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003767IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003768 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3769 nullptr, IBI.getNumOperands()) {
3770 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003771 Use *OL = getOperandList();
3772 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003773 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3774 OL[i] = InOL[i];
3775 SubclassOptionalData = IBI.SubclassOptionalData;
3776}
3777
Chris Lattner3ed871f2009-10-27 19:13:16 +00003778/// addDestination - Add a destination.
3779///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003780void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003781 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003782 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003783 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003784 // Initialize some new operands.
3785 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003786 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003787 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788}
3789
3790/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003791/// indirectbr instruction.
3792void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003793 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3794
3795 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003796 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003797
3798 // Replace this value with the last one.
3799 OL[idx+1] = OL[NumOps-1];
3800
3801 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003802 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003803 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003804}
3805
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003806BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807 return getSuccessor(idx);
3808}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003809unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003810 return getNumSuccessors();
3811}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003812void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003813 setSuccessor(idx, B);
3814}
3815
3816//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003817// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003818//===----------------------------------------------------------------------===//
3819
Chris Lattnerf22be932004-10-15 23:52:53 +00003820// Define these methods here so vtables don't get emitted into every translation
3821// unit that uses these classes.
3822
Pete Cooper75403d72015-06-24 20:22:23 +00003823GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003824 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003825}
3826
Pete Cooper75403d72015-06-24 20:22:23 +00003827BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003828 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003829}
3830
Pete Cooper75403d72015-06-24 20:22:23 +00003831FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003832 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003833}
3834
Pete Cooper75403d72015-06-24 20:22:23 +00003835ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003836 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003837}
3838
Pete Cooper75403d72015-06-24 20:22:23 +00003839ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003840 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003841}
3842
Pete Cooper75403d72015-06-24 20:22:23 +00003843InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003844 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003845}
3846
Pete Cooper75403d72015-06-24 20:22:23 +00003847AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003848 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3849 (Value *)getOperand(0), getAlignment());
3850 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003851 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003852 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003853}
3854
Pete Cooper75403d72015-06-24 20:22:23 +00003855LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003856 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3857 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003858}
3859
Pete Cooper75403d72015-06-24 20:22:23 +00003860StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003861 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003862 getAlignment(), getOrdering(), getSynchScope());
3863
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003867 AtomicCmpXchgInst *Result =
3868 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003869 getSuccessOrdering(), getFailureOrdering(),
3870 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003871 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003872 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003873 return Result;
3874}
3875
Pete Cooper75403d72015-06-24 20:22:23 +00003876AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003877 AtomicRMWInst *Result =
3878 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3879 getOrdering(), getSynchScope());
3880 Result->setVolatile(isVolatile());
3881 return Result;
3882}
3883
Pete Cooper75403d72015-06-24 20:22:23 +00003884FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003885 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3886}
3887
Pete Cooper75403d72015-06-24 20:22:23 +00003888TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003889 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003890}
3891
Pete Cooper75403d72015-06-24 20:22:23 +00003892ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003893 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003894}
3895
Pete Cooper75403d72015-06-24 20:22:23 +00003896SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003897 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003898}
3899
Pete Cooper75403d72015-06-24 20:22:23 +00003900FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003901 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003902}
3903
Pete Cooper75403d72015-06-24 20:22:23 +00003904FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003905 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003906}
3907
Pete Cooper75403d72015-06-24 20:22:23 +00003908UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003909 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003910}
3911
Pete Cooper75403d72015-06-24 20:22:23 +00003912SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003913 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914}
3915
Pete Cooper75403d72015-06-24 20:22:23 +00003916FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003917 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003918}
3919
Pete Cooper75403d72015-06-24 20:22:23 +00003920FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003921 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003922}
3923
Pete Cooper75403d72015-06-24 20:22:23 +00003924PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003925 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003926}
3927
Pete Cooper75403d72015-06-24 20:22:23 +00003928IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003929 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003930}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003931
Pete Cooper75403d72015-06-24 20:22:23 +00003932BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003933 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003934}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003935
Pete Cooper75403d72015-06-24 20:22:23 +00003936AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003937 return new AddrSpaceCastInst(getOperand(0), getType());
3938}
3939
Pete Cooper75403d72015-06-24 20:22:23 +00003940CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003941 if (hasOperandBundles()) {
3942 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3943 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3944 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003945 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003946}
3947
Pete Cooper75403d72015-06-24 20:22:23 +00003948SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003949 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003950}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003951
Pete Cooper75403d72015-06-24 20:22:23 +00003952VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003953 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003954}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955
Pete Cooper75403d72015-06-24 20:22:23 +00003956ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003957 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003958}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003959
Pete Cooper75403d72015-06-24 20:22:23 +00003960InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003961 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003962}
3963
Pete Cooper75403d72015-06-24 20:22:23 +00003964ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003965 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003966}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003967
Pete Cooper75403d72015-06-24 20:22:23 +00003968PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003969
Pete Cooper75403d72015-06-24 20:22:23 +00003970LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003971 return new LandingPadInst(*this);
3972}
3973
Pete Cooper75403d72015-06-24 20:22:23 +00003974ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003975 return new(getNumOperands()) ReturnInst(*this);
3976}
3977
Pete Cooper75403d72015-06-24 20:22:23 +00003978BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003979 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003980}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003981
Pete Cooper75403d72015-06-24 20:22:23 +00003982SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003983
Pete Cooper75403d72015-06-24 20:22:23 +00003984IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003985 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003986}
3987
Pete Cooper75403d72015-06-24 20:22:23 +00003988InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003989 if (hasOperandBundles()) {
3990 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3991 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3992 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003993 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003994}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003995
Pete Cooper75403d72015-06-24 20:22:23 +00003996ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003997
David Majnemer654e1302015-07-31 17:58:14 +00003998CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3999 return new (getNumOperands()) CleanupReturnInst(*this);
4000}
4001
David Majnemer654e1302015-07-31 17:58:14 +00004002CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004003 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004004}
4005
David Majnemer8a1c45d2015-12-12 05:38:55 +00004006CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4007 return new CatchSwitchInst(*this);
4008}
4009
4010FuncletPadInst *FuncletPadInst::cloneImpl() const {
4011 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004012}
4013
Pete Cooper75403d72015-06-24 20:22:23 +00004014UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004015 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004016 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004017}