AutoUpgrade.cpp revision 58d74910c6b82e622ecbb57d6644d48fec5a5c0f
16994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
26994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//
36994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//                     The LLVM Compiler Infrastructure
46994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
76994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//
86994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===----------------------------------------------------------------------===//
96994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//
106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// This file implements the auto-upgrade helper functions
116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//
126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===----------------------------------------------------------------------===//
136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/AutoUpgrade.h"
15d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson#include "llvm/Constants.h"
166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Function.h"
176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Module.h"
186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Instructions.h"
196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Intrinsics.h"
2058d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner#include "llvm/ADT/SmallVector.h"
21ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov#include <cstring>
226994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthusing namespace llvm;
236994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
246994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
25f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengstatic bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
266994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "Illegal to upgrade a non-existent Function.");
276994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
286994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Get the Function's name.
296994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  const std::string& Name = F->getName();
306994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
316994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Convenience
326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  const FunctionType *FTy = F->getFunctionType();
336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Quickly eliminate it, if it's not a candidate.
356994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
366994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
37f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    return false;
386994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
396994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  Module *M = F->getParent();
406994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  switch (Name[5]) {
416994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  default: break;
426994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'b':
436994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the name of the llvm.bswap intrinsic function to only use
446994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  a single type name for overloading. We only care about the old format
456994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
466994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  a '.' after 'bswap.'
476994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,6,"bswap.",6) == 0) {
486994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',11);
496994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
506994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos) {
516994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct the new name as 'llvm.bswap' + '.i*'
526994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,10)+Name.substr(delim));
53f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
54f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
556994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
566994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'c':
606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  We only want to fix the 'llvm.ct*' intrinsics which do not have the
616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  correct return type, so we check for the name, and then check if the
626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  return type does not match the parameter type.
636994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if ( (Name.compare(5,5,"ctpop",5) == 0 ||
646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.compare(5,4,"ctlz",4) == 0 ||
656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.compare(5,4,"cttz",4) == 0) &&
666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        FTy->getReturnType() != FTy->getParamType(0)) {
676994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  We first need to change the name of the old (bad) intrinsic, because
686994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  its type is incorrect, but we cannot overload that name. We
696994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  arbitrarily unique it here allowing us to construct a correctly named
706994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  and typed function below.
716994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      F->setName("");
726994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
736994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Now construct the new intrinsic with the correct name and type. We
746994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  leave the old function around in order to query its type, whatever it
756994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  may be, and correctly convert up to the new type.
76f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = cast<Function>(M->getOrInsertFunction(Name,
77f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
78f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
79f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    (Type *)0));
80f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'p':
856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the llvm.part.select overloaded intrinsic names to only
866994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  use one type specifier in the name. We only care about the old format
876994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  'llvm.part.select.i*.i*', and solve as above with bswap.
886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,12,"part.select.",12) == 0) {
896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',17);
906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos) {
926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct a new name as 'llvm.part.select' + '.i*'
936994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,16)+Name.substr(delim));
94f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
95f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
966994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      break;
986994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the llvm.part.set intrinsics similarly as above, however
1016994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  we care about 'llvm.part.set.i*.i*.i*', but only the first two types
1026994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  must match. There is an additional type specifier after these two
1036994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  matching types that we must retain when upgrading.  Thus, we require
1046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  finding 2 periods, not just one, after the intrinsic name.
1056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,9,"part.set.",9) == 0) {
1066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',14);
1076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos &&
1096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.find('.',delim+1) != std::string::npos) {
1106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct a new name as 'llvm.part.select' + '.i*.i*'
1116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,13)+Name.substr(delim));
112f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
113f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
1146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
1156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      break;
1166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
1176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
119d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case 'x':
120d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // This fixes all MMX shift intrinsic instructions to take a
121d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // v1i64 instead of a v2i32 as the second parameter.
122d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
123d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson        (Name.compare(13,4,"psll", 4) == 0 ||
124d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson         Name.compare(13,4,"psra", 4) == 0 ||
125d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson         Name.compare(13,4,"psrl", 4) == 0)) {
126d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
127d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1);
128d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
129d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      // We don't have to do anything if the parameter already has
130d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      // the correct type.
131d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      if (FTy->getParamType(1) == VT)
132d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson        break;
133d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
134d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  We first need to change the name of the old (bad) intrinsic, because
135d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  its type is incorrect, but we cannot overload that name. We
136d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  arbitrarily unique it here allowing us to construct a correctly named
137d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  and typed function below.
138d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      F->setName("");
139d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
140d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
141d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
142d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  Now construct the new intrinsic with the correct name and type. We
143d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  leave the old function around in order to query its type, whatever it
144d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  may be, and correctly convert up to the new type.
145f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = cast<Function>(M->getOrInsertFunction(Name,
146f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getReturnType(),
147f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
148f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    VT,
149f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    (Type *)0));
150f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
151f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    } else if (Name.compare(5,16,"x86.sse2.movl.dq",16) == 0) {
152f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      // Calls to this intrinsic are transformed into ShuffleVector's.
153f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = 0;
154f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
155d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    }
156f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
157d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    break;
1586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
1596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  This may not belong here. This function is effectively being overloaded
1616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  to both detect an intrinsic which needs upgrading, and to provide the
1626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  upgraded form of the intrinsic. We should perhaps have two separate
1636994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  functions for this.
164f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  return false;
1656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
1666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
167f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengbool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
168f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  NewFn = 0;
169f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
170a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands
171a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  // Upgrade intrinsic attributes.  This does not change the function.
172f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (NewFn)
173f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    F = NewFn;
174a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  if (unsigned id = F->getIntrinsicID(true))
175a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands    F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
176a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  return Upgraded;
177a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands}
178a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands
1796994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
1806994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// upgraded intrinsic. All argument and return casting must be provided in
1816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// order to seamlessly integrate with existing context.
1826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
1836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  Function *F = CI->getCalledFunction();
1846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "CallInst has no function associated with it.");
185f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
186f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (!NewFn) {
187a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng    if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0) {
188f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      std::vector<Constant*> Idxs;
189f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
190f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(Zero);
191f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(Zero);
192f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(Zero);
193f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(Zero);
194f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Value *ZeroV = ConstantVector::get(Idxs);
195f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
196f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.clear();
197f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4));
198f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5));
199f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
200f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3));
201f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      Value *Mask = ConstantVector::get(Idxs);
202f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      ShuffleVectorInst *SI = new ShuffleVectorInst(ZeroV, CI->getOperand(1),
203f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    Mask, "upgraded", CI);
204f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
205f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      // Handle any uses of the old CallInst.
206f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      if (!CI->use_empty())
207f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        //  Replace all uses of the old call with the new cast which has the
208f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        //  correct type.
209f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        CI->replaceAllUsesWith(SI);
210f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
211f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      //  Clean up the old call now that it has been completely upgraded.
212f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      CI->eraseFromParent();
213a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng    } else {
214a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng      assert(0 && "Unknown function for CallInst upgrade.");
215f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    }
216f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    return;
217f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  }
218f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
2196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  switch(NewFn->getIntrinsicID()) {
2206994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  default:  assert(0 && "Unknown function for CallInst upgrade.");
221d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_d:
222d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_q:
223d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_w:
224d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psra_d:
225d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psra_w:
226d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_d:
227d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_q:
228d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_w: {
22958d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Value *Operands[2];
230d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
23158d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Operands[0] = CI->getOperand(1);
232d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
233d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // Cast the second parameter to the correct type.
234d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    BitCastInst *BC = new BitCastInst(CI->getOperand(2),
235d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson                                      NewFn->getFunctionType()->getParamType(1),
236d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson                                      "upgraded", CI);
23758d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Operands[1] = BC;
238d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
239d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Construct a new CallInst
24058d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    CallInst *NewCI = new CallInst(NewFn, Operands, Operands+2,
241d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson                                   "upgraded."+CI->getName(), CI);
242d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    NewCI->setTailCall(CI->isTailCall());
243d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    NewCI->setCallingConv(CI->getCallingConv());
244d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
245d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Handle any uses of the old CallInst.
246d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    if (!CI->use_empty())
247d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  Replace all uses of the old call with the new cast which has the
248d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  correct type.
249d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      CI->replaceAllUsesWith(NewCI);
250d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
251d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Clean up the old call now that it has been completely upgraded.
252d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    CI->eraseFromParent();
253d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    break;
254d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  }
2556994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case Intrinsic::ctlz:
2566994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case Intrinsic::ctpop:
2576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case Intrinsic::cttz:
2586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Build a small vector of the 1..(N-1) operands, which are the
2596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  parameters.
26058d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end());
2616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Construct a new CallInst
2636994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(),
2646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                                   "upgraded."+CI->getName(), CI);
2656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    NewCI->setTailCall(CI->isTailCall());
2666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    NewCI->setCallingConv(CI->getCallingConv());
2676994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2686994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Handle any uses of the old CallInst.
2696994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (!CI->use_empty()) {
2706994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Check for sign extend parameter attributes on the return values.
27158d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner      bool SrcSExt = NewFn->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
27258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner      bool DestSExt = F->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
2736994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2746994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Construct an appropriate cast from the new return type to the old.
2756994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      CastInst *RetCast = CastInst::create(
2766994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            CastInst::getCastOpcode(NewCI, SrcSExt,
2776994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                                                    F->getReturnType(),
2786994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                                                    DestSExt),
2796994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            NewCI, F->getReturnType(),
2806994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            NewCI->getName(), CI);
2816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      NewCI->moveBefore(RetCast);
2826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Replace all uses of the old call with the new cast which has the
2846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  correct type.
2856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      CI->replaceAllUsesWith(RetCast);
2866994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
2876994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Clean up the old call now that it has been completely upgraded.
2896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    CI->eraseFromParent();
2906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
2916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
2926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
2936994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2946994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// This tests each Function to determine if it needs upgrading. When we find
2956994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// one we are interested in, we then upgrade all calls to reflect the new
2966994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// function.
2976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeCallsToIntrinsic(Function* F) {
2986994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
2996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
3006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Upgrade the function and check if it is a totaly new function.
301f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  Function* NewFn;
302f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (UpgradeIntrinsicFunction(F, NewFn)) {
3036994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (NewFn != F) {
3046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      // Replace all uses to the old function with the new one if necessary.
3056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
3066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth           UI != UE; ) {
3076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          UpgradeIntrinsicCall(CI, NewFn);
3096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
3106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      // Remove old function, no longer used, from the module.
3116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      F->eraseFromParent();
3126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
3136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
3146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
3156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
316