AutoUpgrade.cpp revision c25e7581b9b8088910da31702d4ca21c4734c6d7
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"
17f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson#include "llvm/LLVMContext.h"
186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Module.h"
196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Instructions.h"
206994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Intrinsics.h"
2158d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner#include "llvm/ADT/SmallVector.h"
22c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
23ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov#include <cstring>
246994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthusing namespace llvm;
256994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
266994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
27f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengstatic bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
286994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "Illegal to upgrade a non-existent Function.");
296994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
30f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson  LLVMContext* Context = F->getContext();
31f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson
326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Get the Function's name.
336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  const std::string& Name = F->getName();
346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
356994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Convenience
366994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  const FunctionType *FTy = F->getFunctionType();
376994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
386994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Quickly eliminate it, if it's not a candidate.
396994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
406994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
41f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    return false;
426994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
436994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  Module *M = F->getParent();
446994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  switch (Name[5]) {
456994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  default: break;
4628873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang  case 'a':
47e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
48e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    // and atomics with default address spaces to their new names to their new
49e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
50e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    if (Name.compare(5,7,"atomic.",7) == 0) {
5128873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      if (Name.compare(12,3,"lcs",3) == 0) {
5228873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        std::string::size_type delim = Name.find('.',12);
53e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) +
54e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang                   ".p0" + Name.substr(delim+1));
5528873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        NewFn = F;
5628873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        return true;
5728873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      }
5828873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      else if (Name.compare(12,3,"las",3) == 0) {
5928873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        std::string::size_type delim = Name.find('.',12);
60e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        F->setName("llvm.atomic.load.add"+Name.substr(delim)
61e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang                   + ".p0" + Name.substr(delim+1));
6228873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        NewFn = F;
6328873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        return true;
6428873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      }
6528873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      else if (Name.compare(12,3,"lss",3) == 0) {
6628873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        std::string::size_type delim = Name.find('.',12);
67e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        F->setName("llvm.atomic.load.sub"+Name.substr(delim)
68e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang                   + ".p0" + Name.substr(delim+1));
69e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        NewFn = F;
70e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        return true;
71e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang      }
72e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang      else if (Name.rfind(".p") == std::string::npos) {
73e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        // We don't have an address space qualifier so this has be upgraded
74e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        // to the new name.  Copy the type name at the end of the intrinsic
75e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        // and add to it
76e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        std::string::size_type delim = Name.find_last_of('.');
77e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        assert(delim != std::string::npos && "can not find type");
78e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang        F->setName(Name + ".p0" + Name.substr(delim+1));
7928873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        NewFn = F;
8028873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang        return true;
8128873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang      }
8228873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang    }
8328873106309db515d58889a4c4fa3e0a92d1b60eMon P Wang    break;
846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'b':
856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the name of the llvm.bswap intrinsic function to only use
866994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  a single type name for overloading. We only care about the old format
876994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  a '.' after 'bswap.'
896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,6,"bswap.",6) == 0) {
906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',11);
916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos) {
936994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct the new name as 'llvm.bswap' + '.i*'
946994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,10)+Name.substr(delim));
95f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
96f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
986994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
1006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1016994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'c':
1026994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  We only want to fix the 'llvm.ct*' intrinsics which do not have the
1036994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  correct return type, so we check for the name, and then check if the
1046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  return type does not match the parameter type.
1056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if ( (Name.compare(5,5,"ctpop",5) == 0 ||
1066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.compare(5,4,"ctlz",4) == 0 ||
1076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.compare(5,4,"cttz",4) == 0) &&
1086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        FTy->getReturnType() != FTy->getParamType(0)) {
1096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  We first need to change the name of the old (bad) intrinsic, because
1106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  its type is incorrect, but we cannot overload that name. We
1116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  arbitrarily unique it here allowing us to construct a correctly named
1126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  and typed function below.
1136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      F->setName("");
1146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Now construct the new intrinsic with the correct name and type. We
1166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  leave the old function around in order to query its type, whatever it
1176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  may be, and correctly convert up to the new type.
118f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = cast<Function>(M->getOrInsertFunction(Name,
119f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
120f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
121f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    (Type *)0));
122f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
1236994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
1246994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
1256994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1266994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case 'p':
1276994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the llvm.part.select overloaded intrinsic names to only
1286994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  use one type specifier in the name. We only care about the old format
1296994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  'llvm.part.select.i*.i*', and solve as above with bswap.
1306994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,12,"part.select.",12) == 0) {
1316994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',17);
1326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos) {
1346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct a new name as 'llvm.part.select' + '.i*'
1356994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,16)+Name.substr(delim));
136f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
137f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
1386994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
1396994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      break;
1406994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
1416994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1426994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  This upgrades the llvm.part.set intrinsics similarly as above, however
1436994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  we care about 'llvm.part.set.i*.i*.i*', but only the first two types
1446994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  must match. There is an additional type specifier after these two
1456994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  matching types that we must retain when upgrading.  Thus, we require
1466994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  finding 2 periods, not just one, after the intrinsic name.
1476994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (Name.compare(5,9,"part.set.",9) == 0) {
1486994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      std::string::size_type delim = Name.find('.',14);
1496994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1506994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      if (delim != std::string::npos &&
1516994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          Name.find('.',delim+1) != std::string::npos) {
1526994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        //  Construct a new name as 'llvm.part.select' + '.i*.i*'
1536994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        F->setName(Name.substr(0,13)+Name.substr(delim));
154f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        NewFn = F;
155f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        return true;
1566994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
1576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      break;
1586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
1596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
1606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    break;
161d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case 'x':
162d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // This fixes all MMX shift intrinsic instructions to take a
163d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // v1i64 instead of a v2i32 as the second parameter.
164d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
165d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson        (Name.compare(13,4,"psll", 4) == 0 ||
166d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson         Name.compare(13,4,"psra", 4) == 0 ||
16722b942aa4df824adbd3f6eaede53abe451f616e9Evan Cheng         Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
168d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
169f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson      const llvm::Type *VT =
170f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson                        Context->getVectorType(Context->getIntegerType(64), 1);
171d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
172d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      // We don't have to do anything if the parameter already has
173d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      // the correct type.
174d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      if (FTy->getParamType(1) == VT)
175d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson        break;
176d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
177d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  We first need to change the name of the old (bad) intrinsic, because
178d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  its type is incorrect, but we cannot overload that name. We
179d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  arbitrarily unique it here allowing us to construct a correctly named
180d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  and typed function below.
181d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      F->setName("");
182d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
183d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
184d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
185d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  Now construct the new intrinsic with the correct name and type. We
186d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  leave the old function around in order to query its type, whatever it
187d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  may be, and correctly convert up to the new type.
188f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = cast<Function>(M->getOrInsertFunction(Name,
189f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getReturnType(),
190f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    FTy->getParamType(0),
191f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    VT,
192f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng                                                    (Type *)0));
193f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
1944797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng    } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
1954797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng               Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
196e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng               Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
197e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng               Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
198e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng               Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
199e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng               Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
200a31593901de573813d1d8e7884a152011641a713Evan Cheng               Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
201a31593901de573813d1d8e7884a152011641a713Evan Cheng               Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
202a31593901de573813d1d8e7884a152011641a713Evan Cheng               Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
2034797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      // Calls to these intrinsics are transformed into ShuffleVector's.
204f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      NewFn = 0;
205f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      return true;
206d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    }
207f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
208d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    break;
2096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
2106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
2116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  This may not belong here. This function is effectively being overloaded
2126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  to both detect an intrinsic which needs upgrading, and to provide the
2136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  upgraded form of the intrinsic. We should perhaps have two separate
2146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  //  functions for this.
215f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  return false;
2166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
2176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
218f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengbool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
219f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  NewFn = 0;
220f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
221a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands
222a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  // Upgrade intrinsic attributes.  This does not change the function.
223f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (NewFn)
224f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    F = NewFn;
22549de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  if (unsigned id = F->getIntrinsicID())
2260598866c052147c31b808391f58434ce3dbfb838Devang Patel    F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
227a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  return Upgraded;
228a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands}
229a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands
2306994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
2316994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// upgraded intrinsic. All argument and return casting must be provided in
2326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// order to seamlessly integrate with existing context.
2336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
2346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  Function *F = CI->getCalledFunction();
235f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson  LLVMContext* Context = F->getContext();
236f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson
2376994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "CallInst has no function associated with it.");
238f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
239f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (!NewFn) {
2404797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng    bool isLoadH = false, isLoadL = false, isMovL = false;
241e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    bool isMovSD = false, isShufPD = false;
242e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    bool isUnpckhPD = false, isUnpcklPD = false;
243a31593901de573813d1d8e7884a152011641a713Evan Cheng    bool isPunpckhQPD = false, isPunpcklQPD = false;
2444797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng    if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0)
2454797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      isLoadH = true;
2464797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0)
2474797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      isLoadL = true;
2484797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0)
2494797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      isMovL = true;
250e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movs.d") == 0)
251e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      isMovSD = true;
252e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.shuf.pd") == 0)
253e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      isShufPD = true;
254e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckh.pd") == 0)
255e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      isUnpckhPD = true;
256e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckl.pd") == 0)
257e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      isUnpcklPD = true;
258a31593901de573813d1d8e7884a152011641a713Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckh.qdq") == 0)
259a31593901de573813d1d8e7884a152011641a713Evan Cheng      isPunpckhQPD = true;
260a31593901de573813d1d8e7884a152011641a713Evan Cheng    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckl.qdq") == 0)
261a31593901de573813d1d8e7884a152011641a713Evan Cheng      isPunpcklQPD = true;
2624797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng
263e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng    if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
264a31593901de573813d1d8e7884a152011641a713Evan Cheng        isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
265f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      std::vector<Constant*> Idxs;
2664797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      Value *Op0 = CI->getOperand(1);
267e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      ShuffleVectorInst *SI = NULL;
2684797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      if (isLoadH || isLoadL) {
269f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Op1 = Context->getUndef(Op0->getType());
2704797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Value *Addr = new BitCastInst(CI->getOperand(2),
271f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson                                  Context->getPointerTypeUnqual(Type::DoubleTy),
2724797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng                                      "upgraded.", CI);
2734797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
274f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Idx = Context->getConstantInt(Type::Int32Ty, 0);
2754797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
2764797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng
2774797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        if (isLoadH) {
278f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
279f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
2804797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        } else {
281f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
282f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
2834797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        }
284f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Mask = Context->getConstantVector(Idxs);
2854797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
286e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      } else if (isMovL) {
287f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Constant *Zero = Context->getConstantInt(Type::Int32Ty, 0);
2884797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Idxs.push_back(Zero);
2894797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Idxs.push_back(Zero);
2904797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Idxs.push_back(Zero);
2914797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Idxs.push_back(Zero);
292f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *ZeroV = Context->getConstantVector(Idxs);
293f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
2944797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        Idxs.clear();
295f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 4));
296f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 5));
297f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
298f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
299f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Mask = Context->getConstantVector(Idxs);
3004797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng        SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
301a31593901de573813d1d8e7884a152011641a713Evan Cheng      } else if (isMovSD ||
302a31593901de573813d1d8e7884a152011641a713Evan Cheng                 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
303e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        Value *Op1 = CI->getOperand(2);
304e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        if (isMovSD) {
305f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
306f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
307a31593901de573813d1d8e7884a152011641a713Evan Cheng        } else if (isUnpckhPD || isPunpckhQPD) {
308f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
309f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
310e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        } else {
311f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
312f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
313e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        }
314f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Mask = Context->getConstantVector(Idxs);
315e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
316e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      } else if (isShufPD) {
317e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        Value *Op1 = CI->getOperand(2);
318e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue();
319f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, MaskVal & 1));
320f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Idxs.push_back(Context->getConstantInt(Type::Int32Ty,
321f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson                                               ((MaskVal >> 1) & 1)+2));
322f81b57694b3c34a79b1464ffd21e6768c8d22662Owen Anderson        Value *Mask = Context->getConstantVector(Idxs);
323e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng        SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
3244797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng      }
325f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
326e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng      assert(SI && "Unexpected!");
327e716bb1c59fe64c663d1d352e0e3889a28dbaca2Evan Cheng
328f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      // Handle any uses of the old CallInst.
329f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      if (!CI->use_empty())
330f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        //  Replace all uses of the old call with the new cast which has the
331f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        //  correct type.
332f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng        CI->replaceAllUsesWith(SI);
333f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
334f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      //  Clean up the old call now that it has been completely upgraded.
335f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng      CI->eraseFromParent();
336a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng    } else {
337c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin      LLVM_UNREACHABLE("Unknown function for CallInst upgrade.");
338f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    }
339f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng    return;
340f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  }
341f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng
342051a950000e21935165db56695e35bade668193bGabor Greif  switch (NewFn->getIntrinsicID()) {
343c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin  default:  LLVM_UNREACHABLE("Unknown function for CallInst upgrade.");
344d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_d:
345d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_q:
346d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psll_w:
347d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psra_d:
348d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psra_w:
349d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_d:
350d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_q:
351d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  case Intrinsic::x86_mmx_psrl_w: {
35258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Value *Operands[2];
353d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
35458d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Operands[0] = CI->getOperand(1);
355d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
356d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    // Cast the second parameter to the correct type.
357d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    BitCastInst *BC = new BitCastInst(CI->getOperand(2),
358d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson                                      NewFn->getFunctionType()->getParamType(1),
3594797f61657fc32d2e4e806f9f8faadf3fee3e64cEvan Cheng                                      "upgraded.", CI);
36058d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    Operands[1] = BC;
361d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
362d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Construct a new CallInst
363051a950000e21935165db56695e35bade668193bGabor Greif    CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
364051a950000e21935165db56695e35bade668193bGabor Greif                                       "upgraded."+CI->getName(), CI);
365d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    NewCI->setTailCall(CI->isTailCall());
366d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    NewCI->setCallingConv(CI->getCallingConv());
367d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
368d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Handle any uses of the old CallInst.
369d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    if (!CI->use_empty())
370d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  Replace all uses of the old call with the new cast which has the
371d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      //  correct type.
372d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson      CI->replaceAllUsesWith(NewCI);
373d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson
374d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    //  Clean up the old call now that it has been completely upgraded.
375d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    CI->eraseFromParent();
376d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson    break;
377d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson  }
3786994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case Intrinsic::ctlz:
3796994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  case Intrinsic::ctpop:
380051a950000e21935165db56695e35bade668193bGabor Greif  case Intrinsic::cttz: {
3816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Build a small vector of the 1..(N-1) operands, which are the
3826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  parameters.
38358d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end());
3846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
3856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Construct a new CallInst
386051a950000e21935165db56695e35bade668193bGabor Greif    CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
387051a950000e21935165db56695e35bade668193bGabor Greif                                       "upgraded."+CI->getName(), CI);
3886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    NewCI->setTailCall(CI->isTailCall());
3896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    NewCI->setCallingConv(CI->getCallingConv());
3906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
3916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Handle any uses of the old CallInst.
3926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (!CI->use_empty()) {
3936994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Check for sign extend parameter attributes on the return values.
3940598866c052147c31b808391f58434ce3dbfb838Devang Patel      bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
3950598866c052147c31b808391f58434ce3dbfb838Devang Patel      bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
3966994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
3976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Construct an appropriate cast from the new return type to the old.
3987cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif      CastInst *RetCast = CastInst::Create(
3996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            CastInst::getCastOpcode(NewCI, SrcSExt,
4006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                                                    F->getReturnType(),
4016994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                                                    DestSExt),
4026994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            NewCI, F->getReturnType(),
4036994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth                            NewCI->getName(), CI);
4046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      NewCI->moveBefore(RetCast);
4056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
4066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  Replace all uses of the old call with the new cast which has the
4076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      //  correct type.
4086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      CI->replaceAllUsesWith(RetCast);
4096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
4106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
4116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    //  Clean up the old call now that it has been completely upgraded.
4126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    CI->eraseFromParent();
413051a950000e21935165db56695e35bade668193bGabor Greif  }
414051a950000e21935165db56695e35bade668193bGabor Greif  break;
4156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
4166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
4176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
4186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// This tests each Function to determine if it needs upgrading. When we find
4196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// one we are interested in, we then upgrade all calls to reflect the new
4206994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// function.
4216994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeCallsToIntrinsic(Function* F) {
4226994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
4236994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
4246994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // Upgrade the function and check if it is a totaly new function.
425f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  Function* NewFn;
426f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng  if (UpgradeIntrinsicFunction(F, NewFn)) {
4276994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    if (NewFn != F) {
4286994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      // Replace all uses to the old function with the new one if necessary.
4296994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
4306994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth           UI != UE; ) {
4316994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth        if (CallInst* CI = dyn_cast<CallInst>(*UI++))
4326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth          UpgradeIntrinsicCall(CI, NewFn);
4336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      }
4346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      // Remove old function, no longer used, from the module.
4356994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth      F->eraseFromParent();
4366994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth    }
4376994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  }
4386994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth}
439